posted 21 years ago
David:
Dave's post above is right. However, I would describe the situation just slightly differently.
Java does support multidimensional arrays, including 2 and 3-dimensional ones. However, it implements them as arrays of arrays, as Dave points out.
Thus, a 3-dimensional array consists of an array of 2-dimensional arrays, which consists of an array of arrays. Because of this approach to multidimensional arrays, each sub-array can be of a different length. Multidimensional arrays in which not all dimensions are the same length are sometimes called "ragged arrays".
In situations in which you aren't using a "ragged arrays", Java's multidimensional arrays act much like multidimensional arrays in other computer langauges. For example, consider this program. It loads a 3 by 4, two-dimensional table with the values 1 through 12.
It produces the following output.
1 2 3 4
5 6 7 8
9 10 11 12
As the program shows, multidimensional arrays in Java work like one would expect.
However, in your example:
You are creating a ragged array of 2 dimensions. Assuming your initialization, it looks like this
1 2
3
4 5 6
As you can see, it is a 2-dimensional array in which the rows are unequal in length. However, it is still a 2-dimensional array.
For my latest books on Java, including Introducing JavaFX 8 Programming, see HerbSchildt.com