Through the study in the previous section, we know that there is no concept of multi-dimensional arrays in Java, only one-dimensional arrays. We can understand multi-dimensional arrays as arrays of arrays, so an irregular array will be derived.
For example, a regular 4×3 two-dimensional array has 12 elements, but an irregular 4×3 two-dimensional array does not necessarily have that many elements. For example, statically initialize an irregular array:
intArray[][]={{1,2},{3},{4,5,6},{7,8}};
We call arrays of two dimensions and above high-dimensional arrays. Then the high-dimensional array above has 4 elements, but the number of elements of the low-dimensional array is different. The first array has 2 elements, and the second The array has 1 element, the 3rd array has 3 elements, and the 4th array has 2 elements. This is what we call an irregular array.
Dynamically initializing an irregular array is a little more troublesome. It cannot directly use the new int[4][3] statement. We need to initialize the high-dimensional array first, and then initialize the low-dimensional array separately, for example:
intArray[][]=newint[4][];//Initialize the high-dimensional array first to 4Array[0]=newint[2];//Initialize the low-dimensional array Array[1]=newint[1];Array[ one by one 2]=newint[3];Array[3]=newint[2];
Then after the above code initializes the array, we can know that there are not 12 elements, but only 8 elements, where the subscripts are [0][2], [1][1], [1][2] and [ 3][2] does not exist. When we try to access them, an out-of-bounds subscript exception will be thrown.
Note : The subscript out-of-bounds exception (ArrayIndexOutOfBoundsException) is thrown when trying to access a non-existent subscript. For example, assuming a one-dimensional array Array has 12 elements, then the expression Array[12] will cause a subscript out-of-bounds exception. This is because the array subscript starts from 0, and the last element subscript should be the array length minus the array length. 1, so the element accessed by Array[12] does not exist.