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
• Devaka Cooray
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Jeanne Boyarsky
• Tim Cooke
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Tim Moores
• Mikalai Zaikin
• Carey Brown
Bartenders:

# multidimensional array confusion

Ranch Hand
Posts: 65
• Number of slices to send:
Optional 'thank-you' note:
Hi, I need help understanding multidimensonal arrays. I get lost after 2 dimensions.
for example:
int [] [] scores = int[2][3]

I understand this means I have a 2D array object referenced by scores [2][], which is 2 rows long.
Inside each of the 2 rows is a 1D array object -3 columns longs each, referenced by scores [0] and scores[1].

But how do you explain:
int [][][] scores = int[2][3][3]
OR
int [][][][] scores = int[2][3][3][3]

Please excuse me if I my terminology or examples are incorrect.
I read the section in K&B SCJP6 chapter 3 about multidimesional arrays, but I just don't quite get it.

author
Posts: 23945
142
• Number of slices to send:
Optional 'thank-you' note:
The easiest way to think about arrays in Java, is Java does *not* support multidimensions arrays. Multidimensional arrays are just arrays of arrays.

So...

int scores[][] = new int[2][3];

Is just an array of array of ints. The first dereferences will dereference the array of array of ints to just an array of ints... meaning scores[1] is an array of ints.... and scores[1][1] will derefence the array of int to just an int.

Henry

Henry Wong
author
Posts: 23945
142
• Number of slices to send:
Optional 'thank-you' note:

To continue...

int scores[][][][] = new int[2][3][4][3];

Here scores is an array of array of array of array of ints. If you dereference it twice...

scores[1][2]

this represents an array of array of ints.

So...

int oldscores[][] = scores[1][2];

Since scores is an array of array of array of array of ints, dereferencing it twice will yield an array of array of ints, which of course, can be assigned to an array of array of ints.

Henry

Fritz Guerilus
Ranch Hand
Posts: 65
• Number of slices to send:
Optional 'thank-you' note:
Java may not support multidimensional arrays, but its certainly on the test.
I am having difficulty visualizing a 3D array so I can answer the questions.
I'll post the specific question I'm having difficulty with