wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on Multidimensional 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 "Question on Multidimensional Array" Watch "Question on Multidimensional Array" New topic

Question on Multidimensional Array

Yadrif Rifkins

Joined: May 12, 2011
Posts: 4
While studying multidimensional arrays in the K&B book I am having difficultly understanding this example (Page 228).

In the following code scores is three dimensional array (derived from the number of items (comma-separated) between the outer curly braces)

But scores is only declared to be a two dimensional array so I can accept that the dimensions are being set by the list but when the declaration is changed to:

the code does not compile.

I am missing something here. Can someone please explain?

Justin Allen

Joined: Feb 02, 2011
Posts: 18

Yadrif, I was having some problems with this and then I wrote this little test program to help:

So this is how it breaks down:
scores[0] is {5,2,4,7}, scores[1] is {9,2}, and scores[2] is {3,4}.
Then scores[0][0] is 5, scores [0][1] is 2, scores[2][0] is 3 and so on.

The key is that the first dimension of a multi-dimensional array is an array of objects, and the second dimension is the object's themselves in this case, ints.
Yadrif Rifkins

Joined: May 12, 2011
Posts: 4
Thanks for the example.

Here's a sample that shows where I am still confused.

When I declare and initialize the array as (line 14):
int[][][] scores3 = { {1,2,3}, {4,5,6}, {7,8,9} };

The compile complains as follows:
ArrayTest.java:16: incompatible types
found : int
required: int[]
int[][][] scores3 = { {1,2,3}, {4,5,6}, {7,8,9} };
ArrayTest.java:16: incompatible types
found : int
required: int[]
int[][][] scores3 = { {1,2,3}, {4,5,6}, {7,8,9} };
. ^
9 errors

When I declare without the initialization I can use "int[][][]" but why am I not able to use "int[][][]" when I declare and initialize the array?

This only seems to be an issue with three dimensional arrays.

Thanks for the help.
Henry Wong

Joined: Sep 28, 2004
Posts: 19000


A three dimensional array, or more correctly, an array of arrays of arrays needs to be assigned as such. In your example, you just assigned it to an array of arrays, where the outer array happens to have three elements.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 179
As Henry said.

If you're still confused, think of it like this:

int [] = array of ints
int[][] = array of arrays of ints
int[][][] = array of arrays of arrays of ints

// Andreas
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Hey Guys, permit me add some more codes lets see if its clearer. @Andreas I precisely want to add to your code.

@Yadrif, lines 5, 7 and 10 are SIMILAR, if you take a CLOSE look at line 7 you will notice that the SECOND array holds ONE array of ints.

In line 10 the SECOND array holds THREE arrays of ints. (1) {1,2,3}, (2) {1,2,3}, (3) {1,2,3}

I hope this helps.

In Your Pursuit Towards Certification, NEVER Give Up.
I agree. Here's the link: http://aspose.com/file-tools
subject: Question on Multidimensional Array