MultiDimensional Arrays -How many objects are created?
Lucky J Verma
Joined: Apr 11, 2007
I have a doubt in number of objects created in total in case of multidimensional array.
This is the example below i picked from a java tutorial website.
It says 6 objects created in this example , i dint understand how?
Someone please explain to me.
It should be 12 as fas ar i know.I refered to many other websites too.
no of objects=now of rows+1 object to hold rows-reference/count+1 2Dobject array reference
int a2 = new int;
This allocates an int array with 10 rows and 5 columns. As with all objects, the values are initialized to zero (unlike local variables which are uninitialized).
This actually allocates 6 objects: a one-dimensional array of 5 elements for each of the rows, and a one-dimensional array of ten elements, with each element pointing to the appropriate row array.
Joined: Oct 13, 2005
Lucky J Verma wrote:. . .int a2 = new int;
This allocates an int array with 10 rows and 5 columns.
No it doesn't. It creates one array with 10 int members. I make that 11; how did you get 12?
Which website was that; unless they actually said new int they have made a major mistake.
There is no such thing as a multi-dimensional array in Java.
As with all objects, the values are initialized to zero (unlike local variables which are uninitialized).
The  creates 10 5-member arrays, each with 5 0s in.