1

i've had this question ever since i learned two dimensional arrays... and i hope someone can clarify things for me..

here's what i know
int[] x= new int[2];
this one dimensional array has two elements.

int[][] y=new int[2][3];
this two dimensional array has six elements right? (2 x 3 = 6)

but when i look into the way a two dimensional array is actually made,

  1. first a reference type variable named y that can store memory addresses of a two-dimensional array is made in the stack.

  2. then an object with two elements that can store one dimensional memory addresses is made in the heap.

  3. then another 2 one-dimensional arrays are made, each consisting 3 elements that can store int type values. and the memory addresses of these two one dimensional arrays are assigned to the two elements that were made earlier inside the object.
  4. then this object's memory address is assigned to the y variable that is in the stack.

now the problem i have is, only two elements are made inside that object in the beginning right? so doesn't that mean that the two dimensional array has two elements. and the two one-d arrays have 3 elements each?

all of these questions bugs me because y.length = 2 and y[0].length =3

please correct me if i am wrong and hope someone could help me. thanks

Dilini Peiris
  • 446
  • 1
  • 6
  • 16

3 Answers3

2

Yes, you're absolutely right. Java doesn't have 2-dimensional arrays per se. All it has is arrays containing arrays, and syntax sugar (new int[2][3]) to create them.

But you could indeed have an array containing arrays of different lengths:

int[][] array = new int[2][];
array[0] = new int[5];
array[1] = new int[3];
System.out.println(Arrays.deepToString(array));
// prints [[0, 0, 0, 0, 0], [0, 0, 0]]

So it you get an int[][] and want to know how many integers it contains, you need to loop through the outer array and sum the lengths of the inner arrays. Unless you have the guarantee that all inner arrays have the same length. In that case, you can multiply the length of the outer array by the length of any of the inner arrays.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

In Java, multidimensional arrays are actually arrays of arrays. For example, a two dimensional array variable called arr looks like:

int arr[][] = new int[4][5];

This allocates a 4 by 5 array and assigns it to arr. Conceptually, this array will look like the one shown in the Figure: enter image description here

When you allocate memory for a multidimensional array, you need only specify the memory for the first (leftmost) dimension. You can allocate the remaining dimensions separately. For example:

int twoD[][] = new int[4][];
twoD[0] = new int[5];
twoD[1] = new int[5];
twoD[2] = new int[5];
twoD[3] = new int[5];

While there is no advantage to individually allocating the second dimension arrays in this situation, there may be some benefits. Like allocating dimensions manually, you do not need to allocate the same number of elements for each dimension. As stated earlier, since multidimensional arrays are actually arrays of arrays, the length of each array is under your control.

Pial Kanti
  • 1,550
  • 2
  • 13
  • 26
0

You could flatten your multidimensional Array with this method, and call List#size() on it.

It will even flatten nested Lists, Sets, Maps and Queues.

Community
  • 1
  • 1
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124