• 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

Constructing Multidimentional Arrays

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From K&B 5 book.. under Constructing Multidimentional Arrays topic on page 213.. i came across int[][] myArray=new int[3][]; its given in the book that only first brackets are given a size and thats acceptable in java since the JVM needs to know only the size of the object assigned to variable myArray.. but i tried to put the size on right bracket too and i dint get compiler error... i understand that JVM only checks the size in the first bracket but putting the size in the second bracket doesnt give us Compiler error.. what do we do if this kind of question appear in th scjp exam? do we need to get on with it or do we need to select compiler error? NOT mentioned much in K&B further

 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well if there is no compilation error, how can you select Compilation Fails as the answer in the exam?? Anyways, setting the size of the second dimension is optional. So if I write new String[2][] that's also okay, and new String[2][3] is also okay...
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohammad shaid wrote:i came across int[][] myArray=new int[3][]; its given in the book that only first brackets are given a size and thats acceptable in java since the JVM needs to know only the size of the object assigned to variable myArray.. but i tried to put the size on right bracket too and i dint get compiler error.



There is also a bit of syntactic sugar going on -- to make your life easier, and make it easier to read. Basically, when you do this...



You are instantiating an array of size three, which contains references to int arrays, but are assigned to null.

When you do this...



It does the same thing, but it goes one step further. It will then instantiate a bunch of int arrays of size 7, and assign one to each of the three elements of the int array array.

It certainly looks cleaner than the first case, followed by a loop to instantiate the individual elements.

Henry
 
mohammad shaid
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit and Henry...
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry sir really makes us think
thanks for giving the opportunity for attempting a simple program once again
Shaad, here is a sample program that can help a little about the understanding of the arrays
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To let the other foot drop and state what may be obvious, the reason
to specify only the first dimension is to allow variable lengths for the
second, third, etc. dimensions. For example: Jim ... ...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic