This week's book giveaway is in the Testing forum.We're giving away four copies of The Way of the Web Tester: A Beginner's Guide to Automating Tests and have Jonathan Rasmusson on-line!See this thread for details.
Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Question on Multidimensional Array

Greenhorn
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?

Thanks.

Justin Allen
Greenhorn
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.

Greenhorn
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
author
Marshal
Posts: 21742
85
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

Andreas Svenkson
Ranch Hand
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
Posts: 394
• 1
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.