• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Question on Multidimensional Array

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 18
Eclipse IDE Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic