Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

multidimensional array confusion

 
Fritz Guerilus
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21203
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21203
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic