• Post Reply Bookmark Topic Watch Topic
  • New Topic

multi-dimensional array size  RSS feed

 
Steve Harmison
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please tell me why we can do the following:

int[][][] i = new int[10][][];

Surely Java does not know how much space to set aside for the array, as we have left the rightmost arguments blank!
 
Jesus Angeles
Ranch Hand
Posts: 2070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It allows you to declare it later. And each of those 10 doesnt have to be the same parameters.

E.g.

[0] has [3][5]
[1] has [34][2]
[2] has [8][7]
.
.
[9] has [2][63]

E.g.

int[][][] i = new int[10][][];
i[0] = new int [3][5]
i[1] = new int [34][2]
i[2] = new int [8][7]
.
.
i[9] = new int [2][63]
[ September 01, 2007: Message edited by: Jesus Angeles ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Harmison:
...int[][][] i = new int[10][][];

Surely Java does not know how much space to set aside for the array, as we have left the rightmost arguments blank!

Actually, it does know how much space to set aside for "the array."

In Java, multi-dimensional arrays are just arrays of arrays. And the elements of the array are just references -- not the objects themselves. So in this example, "i" simply references a single-dimension array of length 10, where each element is a reference to another array. (At this point, these 10 references are all null because we haven't put anything in "i" yet, but a null reference takes the same space as an object reference.)

In other words, the "size" or length of a multi-dimensional array is simply the first dimension, because it's really no different than a single-dimension array. Additional dimensions specify separate objects (arrays) that are in the first array.
[ September 01, 2007: Message edited by: marc weber ]
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just read in my study guide in the past few days that [] is the same as an object as far as Java is concerned.

Whenever you see something like this, you have to allow for the fact that (like marc weber states) the first [] = new [10] only allocates the first array reference, the remaining two [][] being just places to put information about an arrary once it is created.

I never gave it a great deal of thought, but what Jesus Angeles will be of use in thinking about what is going on.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nicholas Jordan:
...the remaining two [][] being just places to put information about an arrary once it is created...

The empty brackets are necessary because they provide information about the type -- both when declaring the variable and when creating the object. Remember that the type of references an array holds is also part of the array's type. For example, String[] is an array that holds references to Strings. But String[][] is an array that holds references to arrays (that in turn hold references to Strings).
 
Steve Harmison
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!