• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

MultiDimensional Arrays -How many objects are created?

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

"
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic