We know that in Java, we can construct an array of objects as follows:
class A {
int a;
int b;
}
public class Test {
public static void main(String[] args) {
A [] A = new A [2]; // each element in the array is
System. Out. println (A [0]); // output null
}
}
We can see that square brackets after the data type can constitute an array composed of the data type. What if I want to add another dimension? Obviously, you only need to add square brackets to the right of the previous array. For example, if a one-dimensional array can be written as a [], then a two-dimensional array can be written as a [], but which of the two square brackets is the square brackets in a []? If we follow the above logic, it must be the one on the left. For example, a [2] [1] is an array composed of one a [2.
Is that true? Perform the following tests:
class A {
int a;
int b;
}
public class Test {
public static void main(String[] args) {
A[][] a = new A[2][1];
System. Out. println (A. Length); // The output result is 2.
}
}
As shown in the preceding example, [] on the right is the square brackets in. That is, a [2] [1] represents an array composed of two A [1. The basic data type (such as int [] []) is also tested.