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

Question on Multidimensional Array

 
Yadrif Rifkins
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Chrome Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic