Reputation: 11
Assume
double[][][] x = new double[4][5][6]
, why x.length, x[2].length
, and x[0][0].length
are 4, 5, and 6 ? Why [0][0].length
is 6?
Upvotes: 1
Views: 920
Reputation: 20163
To visualize a three-dimensional array, and to comprehend its length, you can think of it in terms of literal physical dimensions (remember the book Flat Land?).
One object:
.
An array of objects:
.............
The length of this array (array.length
) is how many elements, or dots, counting from left to right.
A two-dimensional array of objects:
.............
.............
.............
.............
.............
.............
Each element in the previous array now has its own array, going down. The length of the first element in this array (array[0].length
, the top-left-most) is counting the dots top to bottom.
A three dimensional array is like a cube (this is all assuming uniform lengths among elements)
.............
............. .
............. . .
............. . . .
............. . . .
............. . . .
. . . . . . . . .
. . . . . . . ..
. . . . . . . .
Each element in the previous array now has its own array (going deep "into" the screen). The length of the first element in this array (array[0][0].length
, the top-left-most element) is the number of dots deep.
Not once have I (directly) used a three dimensional array.
Upvotes: 5
Reputation: 1206
When working with multi-dimensional arrays, it is useful to think of them as an "array-of-arrays." That is to say, double[]
is an array of doubles
, double[][]
would be an array of double[]
and double[][][]
is an array made of arrays of arrays of doubles. So let's break down your example:
double[][][] x = new double[4][5][6];
Working backwards, we can think of this as being an array of 6 arrays of double[][]
, each of which contains 5 arrays of double[]
. Each of those arrays contains 4 doubles. Confused yet?
Another thing that might help to see the relationship is to write this using parentheses:
(((double[])[])[])
Now, when you access x[2]
, you are actually retrieving an array of arrays at index 2
of x
, and that array has length 5.
When you access x[0][0]
, this is still just another array (remember, we build up our array as an array of arrays), which you declared in your new
statement to have length 6
. That is to say, x[0][0]
will not contain a number, but will contain array of type double[][]
, which has a length of 6
.
Upvotes: 0
Reputation: 10945
When you declare an array such as double[] x = new double[5] you are saying you want a 5-long array of double, i.e.
(type) x = new (type)[size];
So when you declare double[][][] x = new double[4][5][6] you are saying you want a 6-long array of double[][]:
(double[][])[] x = new (double[4][5])[6];
Upvotes: 1
Reputation: 3912
With new double[4][5][6]
what you have is 20 arrays of length 6. They are addressable as:
x[0][0] // array of length 6
x[0][1] // another array of length 6 here
x[0][2] // etc...
x[0][3]
x[0][4]
x[1][0]
x[1][1]
x[1][2]
x[1][3]
x[1][4]
x[2][0]
x[2][1]
x[2][2]
x[2][3]
x[2][4]
x[3][0]
x[3][1]
x[3][2]
x[3][3]
x[3][4]
Upvotes: 1