aspose file tools*
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 Java 8 in Action this week in the Java 8 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: 18138
    
  39

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: 18138
    
  39


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
 
Similar Threads
Two dimensional array
An object to hold several arrays
Assigning array to existing array reference.
Arrays doubt
generate random number into 2d arrays