wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes multidimensional array confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "multidimensional array confusion" Watch "multidimensional array confusion" New topic
Author

multidimensional array confusion

Fritz Guerilus
Ranch Hand

Joined: Jun 20, 2009
Posts: 65
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.

Please help
-Thanks in advance


SCJP 6.0
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18754
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18754
    
  40


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

Joined: Jun 20, 2009
Posts: 65
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: multidimensional array confusion