aspose file tools
The moose likes Java in General and the fly likes MultiDimensional Arrays -How many objects are created? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "MultiDimensional Arrays -How many objects are created?" Watch "MultiDimensional Arrays -How many objects are created?" New topic
Author

MultiDimensional Arrays -How many objects are created?

Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 277
Hi,
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[10][5];
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.

"
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
Lucky J Verma wrote:. . .int[][] a2 = new int[10][5];
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[5][10] 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 [10][5] creates 10 5-member arrays, each with 5 0s in.
 
I agree. Here's the link: jrebel
 
subject: MultiDimensional Arrays -How many objects are created?
 
Similar Threads
Multidimensional arrays
sorting multidimensional arrays (int)
HELP!! Multidimensional Array Migraine
MultiDimensional Arrays
Constructing multidimensional arrays