In this lesson of our Java Course, we will learn about the multidimensional Array in Java. In Java, an array is a collection of elements of the same data type. A multidimensional array is an array that contains one or more arrays, and each of these arrays can contain one or more arrays, and so on. Multidimensional arrays are often used to represent tables or matrices of data.
1. Initialization of Java Array
There are two ways to declare a multidimensional array in Java:
- Using the syntax
type[][] arrayName
. - Using the syntax
type arrayName[][]
Let’s see how these 2 types are declared and initialized in Java:
int[][] myArray;
//or
int myArray[][];
Both declarations create a reference to a 2D array. To actually create the array, we need to use the new keyword and specify the size of each dimension. For example, to create a 2D array with 3 rows and 4 columns, we can use the following code:
int[][] myArray = new int[3][4];
We can also create a multidimensional array in Java using an array literal, like this:
class JavaMultiDimensionExample {
public static void main(String[] args) {
int[][] myArray = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11,12 }
};
}
}
This creates a 3×4 array with the following contents:
1 2 3 4
5 6 7 8
9 10 11 12
2. Accessing the elements of a 2D array
We can access the elements of a multidimensional array using the same syntax as we would use for a one-dimensional array. For example, to access the element in the second row and third column of myArray
, we can use the following code:
class JavaMultiDimensionExample {
public static void main(String[] args) {
int[][] myArray = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11,12 }
};
int element = myArray[1][2];
}
}
This will assign the value 7
to the variable element
.
3. Accessing the elements of a 3D array
It’s also possible to create arrays with more than two dimensions. For example, we can create a 3D array by using the following syntax:
int[][][] myArray = new int[3][4][5];
This creates a 3x4x5
array, which you can think of as a cube with 3 layers, 4 rows, and 5 columns. You can access the elements of this array using three indices, like this:
int element = myArray[1][2][3];
This will access the element in the second layer, third row, and fourth column of the array. Multidimensional arrays can be very useful for storing and manipulating large amounts of data. However, they can also be confusing to work with, especially for beginners. It’s important to take the time to understand how multidimensional array work and practice using them in your code.
4. Looping through a Multidimensional Array in Java
When working with a multidimensional array in Java, it’s often necessary to loop through the elements of the array to perform some operation on each element. There are several ways to loop through a multidimensional array in Java. We can choose any method that will depend on the structure of the array and the operation we want to perform.
One common way to loop through a 2D array is to use nested for loops. The outer loop iterates through the rows of the array, and the inner loop iterates through the columns. Here’s an example:
class JavaMultiDimensionExample {
public static void main(String[] args) {
int[][] myArray = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11,12 }
};
for (int i = 0; i < myArray.length; i++) {
for (int j = 0; j < myArray[i].length; j++) {
System.out.print(myArray[i][j] + " ");
}
System.out.println();
}
}
}
The above code creates a 3×3 array and then uses nested for loops to print the contents of the array, with each row on a separate line. The output will be:
1 2 3
4 5 6
7 8 9
Another way to iterate over the multi-dimensional array is using the for-each loop, which provides a simple and readable way to access each element in the array.
class JavaMultiDimensionExample {
public static void main(String[] args) {
int[][] myArray = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11,12 }
};
for (int[] row : myArray) {
for (int element : row) {
System.out.print(element + " ");
}
System.out.println();
}
}
}
For a 3D array, we can use nested for loops with three indices, one for each dimension. Here is an example:
int[][][] myArray = new int[3][4][5];
for (int i = 0; i < myArray.length; i++) {
for (int j = 0; j < myArray[i].length; j++) {
for (int k = 0; k < myArray[i][j].length; k++) {
System.out.print(myArray[i][j][k] + " ");
}
}
System.out.println();
}
This will iterate through each element in the 3D array, and with the above example, it will print nothing since we have initialized no elements in the array.
Summary
Regardless of the method we choose to loop through a multidimensional array in Java, it’s important to understand the structure of the array and the indexing scheme used to access its elements. With a good understanding of how multidimensional arrays work and practice using them, we can write efficient and easy-to-read code that makes working with these arrays easier.
As Always, the source code for this course is available on GitHub Repository.