Click to enter _ more _java thousand ask
1. How multidimensional arrays are stored in memory
Learn how a one-dimensional array is stored see here: How the Java array is stored in memory
From the bottom, array elements can hold reference types, and arrays are also one of the reference types. That is, inside an array element can also contain an array (such as int[][] numbers = new int[length][]), that is, a two-dimensional array can be treated as a one-dimensional array (array length).
Learn the differences between arrays and normal reference objects in memory look here: What is the difference between an array and a reference variable of a normal object?
From this we conclude that any multidimensional array (dimension n,n>1) can be treated as a one-dimensional array whose array elements are n-1-dimensional arrays. A multidimensional array is stored in memory with a reference type array stored in memory.
Learn about the storage of reference type arrays in memory look here: how arrays of reference types are stored in memory
Take a two-dimensional array as an example:
Public classTestmultiarray { Public Static void Main(string[] args) {//1. Define a two-dimensional array int[] numbers;//2. Allocating memory spaceNumbers =New int[2][];//You can treat numbers as a one-dimensional array for(inti =0; i < numbers.length; i++) {System. out. print (Numbers[i] +",");//Null,null,null} System. out. println ("");//3. Specify values for an array elementnumbers[0] =New int[2]; numbers[0][1] =1; numbers[1] =New int[2]; numbers[1][0] = One; numbers[1][1] = the; for(inti =0; i < numbers.length; i++) { for(intj =0; J < Numbers[i].length; J + +) {System. out. print (Numbers[i][j] +","); } System. out. println (""); } }}
The results are as follows:
Null,null,
0,1,
11,15,
Show this array in memory in a diagram:
Java thousand ask _06 data structure (018) _ Multidimensional array How to store in memory