You can easily create multidimensional arrays in Java:
: 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]);
A-varied-length array with vectors:int[][][] a3 = new Int[prand (7)][][];
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 (int i = 0; i < a3.length. i++) for (int j = 0; J < A3[i].length; J +) for (int k = 0; K < a
3[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); }
} ///:~
Length is used in the code used for printing, 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 group:
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 assigned with new. Here, the entire array is allocated immediately:
int[][][] A2 = new Int[2][2][4];
But the third example reveals that each vector that makes up a matrix can have any length:
int[][][] a3 = new Int[prand (7)][][];
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 an array created by the first new, the length of its first element is random and the length of the other elements is undefined. The second new in the For loop fills in the element, but maintains the indeterminate state of the third index-until it encounters a third new.
According to the output, you can see that if the initialization value is not explicitly specified, the array value is automatically initialized to 0.
You can use similar tables to handle arrays of non-base type objects. As you can see from the fourth example, it shows us the ability to collect multiple new expressions with curly braces:
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 basic 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.