Java Reference
In-Depth Information
Pay special attention to the way length is used with the two-dimensional array table . As
explained, a two-dimensional array is an array of arrays. Thus, when the expression
is used, it obtains the number of arrays stored in table, which is 3 in this case. To obtain the
length of any individual array in table, you will use an expression such as this,
which, in this case, obtains the length of the first array.
One other thing to notice in LengthDemo is the way that list.length is used by the for
loops to govern the number of iterations that take place. Since each array carries with it its
own length, you can use this information rather than manually keeping track of an array's
size. Keep in mind that the value of length has nothing to do with the number of elements
that are actually in use. It contains the number of elements that the array is capable of hold-
ing.
The inclusion of the length member simplifies many algorithms by making certain types
of array operations easier—and safer—to perform. For example, the following program
uses length to copy one array to another while preventing an array overrun and its attend-
ant runtime exception.
Search WWH ::




Custom Search