java- how many references there is this three-dimensional array?

Assume the following array

 

 

int myarray [][][] = new int [12][12][3];

This statement generates an array of how many references?

I think it should be 12 * 12 * 3, since the number of elements in the array can be stored.

However, my colleagues believe there are 12 12 * 12, because there are 12 direct references to the "myarray", whereas the second dimension into the third dimension points to 144 references.

But how this reference number stored in the array is less than the actual value of it?

Best answer

Let us be clear about Java programmers call "reference" mean reference values ​​refer to an object, including an array instead of the original value, such as int, int type does not reference any other objects -.. There is only one value 12 * 12 * 3 values, but not the same as the number of references.

 

Each lowest level of the array are not cited because the basic type is primitive -int. This means that the last three length has no effect on the number of references.

In Java, multidimensional arrays are implemented as arrays of arrays. 12 represents the middle value of 12 were cited an array of length 3. The first reference value of 12 represents 12 respectively 12 array of intermediate values ​​described. Your colleagues very close , which means that the multi-dimensional array comprising a number of references 12 12 * 12 or 156. in 3D array, which is 12 12 2D array of external references, plus 144 144 1D array internal reference. including references to myArray itself, 157.

Published 540 original articles · won praise 0 · Views 1963

Guess you like

Origin blog.csdn.net/weixin_44109689/article/details/103934305