Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two dimensional array

 
Garlapati Ravi
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • 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!
 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, what you did made scores a jagged array i.e. different dimensions will have different sizes...
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is no such thing as a two dimensional array in Java . instead use Array of Arrays
 
S Ali
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • 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.
 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • 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
  • 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: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic