You can see the code extracted from thinking in Java to understand, even. Sub-arrays of multidimensional arrays do not need to wait long
: Multidimarray.java
Creating multidimensional arrays.
Import java.util.*;
public class Multidimarray {
static random Rand = new Random ();
static int prand (int mod) {
Return Math.Abs (Rand.nextint ())% mod + 1;
}
public static void Main (string[] args) {
int[][] A1 = {
{1, 2, 3,},
{4, 5, 6,},
};
for (int i = 0; i < a1.length; i++)
for (int j = 0; J < A1[i].length; J + +)
PRT ("a1[" + i + "[" + + j +)
"] =" + a1[i][j]);
-Array with fixed length:
int[][][] A2 = new Int[2][2][4];
for (int i = 0; i < a2.length; i++)
for (int j = 0; J < A2[i].length; J + +)
for (int k = 0; k < a2[i][j].length;
k++)
PRT ("a2[" + i + "[" +
J + "[" + K +
"] =" + a2[i][j][k]);
Vectors array with varied-length:
int[][][] a3 = new Int[prand (7)][][];
for (int i = 0; i < a3.length; i++) {
120
A3[i] = new Int[prand (5) [];
for (int j = 0; J < A3[i].length; J + +)
A3[I][J] = new Int[prand (5)];
}
for (int i = 0; i < a3.length; i++)
for (int j = 0; J < A3[i].length; J + +)
for (int k = 0; k < a3[i][j].length;
k++)
PRT ("a3[" + i + "[" +
J + "[" + K +
"] =" + a3[i][j][k]);
Array of Non-primitive objects:
Integer[][] A4 = {
{New Integer (1), new Integer (2)},
{New Integer (3), new Integer (4)},
{New Integer (5), new Integer (6)},
};
for (int i = 0; i < a4.length; i++)
for (int j = 0; J < A4[i].length; J + +)
PRT ("a4[" + i + "[" + + j +)
"] =" + a4[i][j]);
Integer[][] A5;
A5 = new integer[3][];
for (int i = 0; i < a5.length; i++) {
A5[i] = new INTEGER[3];
for (int j = 0; J < A5[i].length; J + +)
A5[I][J] = new Integer (I*J);
}
for (int i = 0; i < a5.length; i++)
for (int j = 0; J < A5[i].length; J + +)
PRT ("a5[" + i + "[" + + j +)
"] =" + a5[i][j]);
}
static void Prt (String s) {
System.out.println (s);
}
} ///:~
The code used for printing uses length, so it does not have to rely on a fixed array size.
The first example shows a multidimensional array of basic data types. We can use curly braces to set the bounds of each vector within the array:
int[][] A1 = {
{1, 2, 3,},
{4, 5, 6,},
};
Each square bracket pair moves us to the next level of the array.
The second example shows a three-dimensional array allocated with new. Here, the entire array is allocated immediately:
int[][][] A2 = new Int[2][2][4];
But the third example shows that each vector that makes up the matrix can have any length:
int[][][] a3 = new Int[prand (7)][][];
121
for (int i = 0; i < a3.length; i++) {
A3[i] = new Int[prand (5) [];
for (int j = 0; J < A3[i].length; J + +)
A3[I][J] = new Int[prand (5)];
}
For the first new array created, the length of its first element is random, and the length of the other element is undefined. Within the For loop
The second new will fill in the element, but leave the third index undecided-until it touches the third new one.
According to the output, you can see that if the initialization value is not explicitly specified, the array value is automatically initialized to 0.
An array of non-primitive type objects can be processed in a similar form. As can be seen from the fourth example, it demonstrates to us the collection of a number of curly braces
The ability of the new expression:
Integer[][] A4 = {
{New Integer (1), new Integer (2)},
{New Integer (3), new Integer (4)},
{New Integer (5), new Integer (6)},
};
The fifth example shows how to gradually build an array of objects of a non-primitive type:
Integer[][] A5;
A5 = new integer[3][];
for (int i = 0; i < a5.length; i++) {
A5[i] = new INTEGER[3];
for (int j = 0; J < A5[i].length; J + +)
A5[I][J] = new Integer (I*J);
}
I*j just put an interesting value in the integer.
The length of the array in Java is not compiled as specified, because it is an object