English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn to use2multidimensional arrays and3Java multidimensional arrays.
Before learning multidimensional arrays, please make sure you understandJava Arrays}
A multidimensional array is an array of arrays. Each element of a multidimensional array is an array itself. For example,
int[][] a = new int[3][4];
Here, we create a multidimensional array named a. It is a two-dimensional array that can accommodate up to12elements,
Remember, Java uses zero-based indexing, which means that the index of an array in Java starts from 0, not from1Start.
Let's take another example of a multidimensional array. This time we will create a3A multidimensional array. For example,
String[][][] data = new String[3][4][2];
Here, data is a three-dimensional array that can accommodate up to24(3*4*2elements of type String.
This is how we initialize a two-dimensional array in Java.
int[][] a = { {1, 2, 3}, {4, 5, 6, 9}, {7}, };
As we have seen, each element of a multidimensional array is an array itself. And, unlike C / C ++is different, each row of a multidimensional array in Java can have a different length.
class MultidimensionalArray { public static void main(String[] args) { //Create a two-dimensional array int[][] a = { {1, 2, 3}, {4, 5, 6, 9}, {7}, }; //Calculate the length of each row System.out.println("The1Row length: " + a[0].length); System.out.println("The2Row length: " + a[1].length); System.out.println("The3Row length: " + a[2].length); } }
Output:
The1Row length: 3 The2Row length: 4 The3Row length: 1
In the above example, we created a multidimensional array named a because each component of a multidimensional array is also an array (a[0], [1] and a[2] is also an array).
Here, we use the length property to calculate the length of each row.
class MultidimensionalArray { public static void main(String[] args) { int[][] a = { {1, -2, 3}, {-4, -5, 6, 9}, {7}, }; for(int i = 0; i < a.length; ++i) { for(int j = 0; j < a[i].length; ++j) { System.out.println(a[i][j]); } } } }
Output:
1 -2 3 -4 -5 6 9 7
We can also usefor ... each loopto access the elements of a multidimensional array. For example,
class MultidimensionalArray { public static void main(String[] args) { //Create a two-dimensional array int[][] a = { {1, -2, 3}, {-4, -5, 6, 9}, {7}, }; //First, the for...each loop accesses a single array //Within a two-dimensional array for(int[] innerArray: a) { //The second for...each loop accesses each element within the row for(int data: innerArray) { System.out.println(data); } } } }
Output:
1 -2 3 -4 -5 6 9 7
In the above example, we created a two-dimensional array named a and then accessed each element of the array using a for loop and a for...each loop.
Let's see how to initialize a three-dimensional array in Java?3the d array. We can initialize a similar one like2the d array3the d array. For example,
// test is a 3d array int[][][] test = { { {1, -2, 3}, {2, 3, 4} }, { {-4, -5, 6, 9}, {1}, {2, 3} } };
基本上,3The d array is2the d array can also vary in length like a two-dimensional array. The rows of the three-dimensional array can also vary in length like a two-dimensional array.
class ThreeArray { public static void main(String[] args) { // Create a three-dimensional array int[][][] test = { { {1, -2, 3}, {2, 3, 4} }, { {-4, -5, 6, 9}, {1}, {2, 3} } }; //iteration of the for..each loop3elements of the d array for (int[][] array2D: test) { for (int[] array1D: array2D) { for (int item : array1D) { System.out.println(item); } } } } }
Output:
1 -2 3 2 3 4 -4 -5 6 9 1 2 3