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

# Question on Multidimensional Array

Greenhorn
Posts: 4
• Number of slices to send:
Optional 'thank-you' note:
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?

Thanks.

Greenhorn
Posts: 18
• Number of slices to send:
Optional 'thank-you' note:
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
Greenhorn
Posts: 4
• Number of slices to send:
Optional 'thank-you' note:
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.

author
Posts: 23951
142
• Number of slices to send:
Optional 'thank-you' note:
Try....

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.

Henry

Ranch Hand
Posts: 179
• Number of slices to send:
Optional 'thank-you' note:
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

Ranch Hand
Posts: 394
• 1
• Number of slices to send:
Optional 'thank-you' note:
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.

reply
Similar Threads