Java thousand ask _06 data structure (018) _ Multidimensional array How to store in memory

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.