Java Reference
In-Depth Information
The use of irregular (or ragged) multidimensional arrays is not recommended for most
applications, because it runs contrary to what people expect to find when a multidimen-
sional array is encountered. However, irregular arrays can be used effectively in some situ-
ations. For example, if you need a very large two-dimensional array that is sparsely popu-
lated (that is, one in which not all of the elements will be used), an irregular array might be
a perfect solution.
Arrays of Three or More Dimensions
Java allows arrays with more than two dimensions. Here is the general form of a multidi-
mensional array declaration:
type name [ ][ ]…[ ] = new type [ size1 ][ size2 ]…[ sizeN ];
For example, the following declaration creates a 4 × 10 × 3 three-dimensional integer array.
Initializing Multidimensional Arrays
A multidimensional array can be initialized by enclosing each dimension's initializer list
within its own set of curly braces. For example, the general form of array initialization for
a two-dimensional array is shown here:
Here, val indicates an initialization value. Each inner block designates a row. Within each
row, the first value will be stored in the first position of the subarray, the second value in
the second position, and so on. Notice that commas separate the initializer blocks and that
a semicolon follows the closing }.
Search WWH ::




Custom Search