After using a two-dimensional array for a long time, I suddenly found that, except for one problem, he and I did not understand it very much. So to write this article, I will focus on the dimensions of a two-dimensional array;
first, let's look at a piece of code:
public class MyLength {
public static void main(String[] args) {
int[][] arr=new int[2][4];
System.out.println(arr.length);
System.out.println(arr[0].length);
}
}
What do you think of arr in the code? We generally think of a two-dimensional array as a matrix. But in fact the array is not like this in memory.
Let's take a look at the results of this execution first.
In this result, we find that the result of arr.length is 2. The result of arr[0].length is 4.
What do these two numbers represent?
int[][] arr=new int[2][4];
In this code, the first number represents the high-dimensionality of the array, and the second one represents the low-dimensionality.