aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Two dimensional array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Two dimensional array" Watch "Two dimensional array" New topic
Author

Two dimensional array

Garlapati Ravi
Ranch Hand

Joined: Mar 05, 2008
Posts: 171

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

Joined: Aug 03, 2008
Posts: 9321
    
  17

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


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

there is no such thing as a two dimensional array in Java . instead use Array of Arrays
S Ali
Ranch Hand

Joined: Aug 23, 2009
Posts: 129
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.


SCJP 6
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

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.C , SCJP6-93%
(Born to Win)
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

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

Joined: Aug 03, 2008
Posts: 9321
    
  17

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

Joined: Mar 05, 2008
Posts: 171
Thank you all Guys, all your replies were helpful! i have to stick this point in my mind "Array of Arrays"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Two dimensional array