This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes multi-dimensional array size Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "multi-dimensional array size" Watch "multi-dimensional array size" New topic
Author

multi-dimensional array size

Steve Harmison
Greenhorn

Joined: Sep 01, 2007
Posts: 4
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

Joined: Feb 26, 2005
Posts: 2038
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

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
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.


"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Sep 01, 2007
Posts: 4
Thanks everyone.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: multi-dimensional array size
 
Similar Threads
Simple question--
Sierra Bates SCJP 6 Ch. 3 No. 3
Please hava a look at this question about array !!!
Object o = new int[10] ??
Guess the answer and please explain