Java Reference
In-Depth Information
When you allocate memory for a multidimensional array, you need to specify only the
memory for the first (leftmost) dimension. You can allocate the remaining dimensions sep-
arately. For example, the following code allocates memory for the first dimension of table
when it is declared. It allocates the second dimension manually.
Although there is no advantage to individually allocating the second dimension arrays in
this situation, there may be in others. For example, when you allocate dimensions separ-
ately, you do not need to allocate the same number of elements for each index. Since mul-
tidimensional arrays are implemented as arrays of arrays, the length of each array is under
your control. For example, assume you are writing a program that stores the number of pas-
sengers that ride an airport shuttle. If the shuttle runs 10 times a day during the week and
twice a day on Saturday and Sunday, you could use the riders array shown in the following
program to store the information. Notice that the length of the second dimension for the
first five indices is 10 and the length of the second dimension for the last two indices is 2.
Search WWH ::




Custom Search