• 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

Two dimensional array

 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am trying to understand this snippet, please correct me if i am wrong.
int[][] scores = new int[3][3];
123
456
789
scores[0] = new int[1]; // does this line converts above [3][3] to below structure, will 2 and 3 cells disappears after this line ?
1
456
789

because i am getting java.lang.ArrayIndexOutOfBoundsException: 1 at scores[0][1] = new Integer(1);

please clarify. Thanks!
 
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
Yes, what you did made scores a jagged array i.e. different dimensions will have different sizes...
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is no such thing as a two dimensional array in Java . instead use Array of Arrays
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its because you assigned scores[0] to an int array with length 1. keep in mind that scores[0] is only a reference to another array that's how java is different from other languages.
 
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As our friend Ankit said, there is no multidimensional array in java.....

The multi-dimensionality can be achieved through Array of Arrays.............


Here you have created

1)3 array objects each of which in turn can hold 3 integer array objects
2)You have initialized those 3 integer array objects also using new operator

To be clear,

scores[0]--->holds an int[] array object
scores[1]--->holds an int[] array object
scores[2]--->holds an int[] array object

each int[] object is also initialized with each element(integer) in it initialized to zero...

There is nothing wrong if you initialize like,

and then assign int[] to each of the score array objects(array of array).....

Hope you will understand very clearly................
 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:Yes, what you did made scores a jagged array i.e. different dimensions will have different sizes...



friend he hasn't created a jagged array in his third line i suppose....

but he has re-assigned the first score array element with a different int[] object......and hence after this statement it becomes a jagged array.....

 
Ankit Garg
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

friend he hasn't created a jagged array in his third line i suppose....
but he has re-assigned the first score array element with a different int[] object......and hence after this statement it becomes a jagged array.....


What I meant was the same, maybe my language was not clear.

As our friend Ankit said, there is no multidimensional array in java.


seetharaman said that, so credit goes to him not me
 
Garlapati Ravi
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all Guys, all your replies were helpful! i have to stick this point in my mind "Array of Arrays"
 
reply
    Bookmark Topic Watch Topic
  • New Topic