Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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: 74065
332
  • 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.
 
Don't count your weasels before they've popped. And now for a mulberry bush related tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic