Java Reference
In-Depth Information
specified iterator. Every Java array is an iterator, so this type of loop can be used
whenever we want to process every element stored in an array.
The square brackets used to indicate the index of an array are treated as an
operator in Java. Therefore, just like the + operator or the <= operator, the index
operator ( [] ) has a precedence relative to the other Java operators that determines
when it is executed. It has the highest precedence of all Java operators.
Bounds Checking
The index operator performs automatic bounds checking , which ensures that the
index is in range for the array being referenced. Whenever a reference to an array
element is made, the index must be greater than or equal to zero and
less than the size of the array. For example, suppose an array called
prices is created with 25 elements. The valid indexes for the array
are from 0 to 24. Whenever a reference is made to a particular ele-
ment in the array (such as prices[count] ), the value of the index
is checked. If it is in the valid range of indexes for the array (0 to
24), the reference is carried out. If the index is not valid, an exception called
ArrayIndexOutOfBoundsException is thrown.
Of course, in our programs we'll want to perform our own bounds checking.
That is, we'll want to be careful to remain within the bounds of the array and
process every element we intend to. Because array indexes begin at zero and go
up to one less than the size of the array, it is easy to create off-by-one errors in
a program, which are problems created by processing all but one element or by
attempting to index one element too many.
One way to check for the bounds of an array is to use the length constant, which
is held in the array object and stores the size of the array. It is a public constant and
therefore can be referenced directly. For example, after the array prices is created
with 25 elements, the constant prices.length contains the value 25. Its value is set
once when the array is first created and cannot be changed. The length constant,
which is an integral part of each array, can be used when the array size is needed
without having to create a separate constant. Remember that the length of the array is
the number of elements it can hold, thus the maximum index of an array is length-1 .
Let's look at another example. The program shown in Listing 8.2 reads 10
integers into an array called numbers , and then prints them in reverse order.
Note that in the ReverseOrder program, the array numbers is declared to have
10 elements and therefore is indexed from 0 to 9. The index range is controlled in
the for loops by using the length field of the array object. You should carefully
set the initial value of loop control variables and the conditions that terminate
loops to guarantee that all intended elements are processed and only valid indexes
are used to reference an array element.
KEY CONCEPT
Bounds checking ensures that an
index used to refer to an array ele-
ment is in range.
 
Search WWH ::




Custom Search