Sub topic 3
Indexes, Length, and Bounds
An array is a fixed-size group of values. Each value is called an element. An index is a number that tells Java which element you want.
Java starts counting indexes at 0, not 1. For this array, the index of the first score is 0:
| Index | Score |
|---|---|
| 0 | 80 |
| 1 | 90 |
| 2 | 100 |
The bounds of an array are the limits on its valid indexes. Here, the valid indexes are 0 through 2.
Read and change an element
Put an index in square brackets after the array name. For example, scores[0] reads the first element. You can also use an assignment, such as scores[1] = 95, to change an element.
Save this program in a file named Main.java:
public class Main {
public static void main(String[] args) {
int[] scores = {80, 90, 100};
System.out.println("Length: " + scores.length);
System.out.println("First: " + scores[0]);
scores[1] = 95;
System.out.println("Changed: " + scores[1]);
System.out.println("Last: " + scores[scores.length - 1]);
int index = 3;
if (index >= 0 && index < scores.length) {
System.out.println("At index: " + scores[index]);
} else {
System.out.println("Index is outside the array.");
}
}
}Compile and run it with:
javac Main.java
java MainThe output is:
Length: 3
First: 80
Changed: 95
Last: 100
Index is outside the array.Changing scores[1] replaces the value at index 1. It does not change the array’s size or the other scores.
Use length to find the limits
The expression scores.length gives the number of elements in scores. It is written without parentheses.
For an array with three elements, the length is 3, but the last index is 2. In general, the last index of a nonempty array is its length minus 1. That is why the program uses scores[scores.length - 1] to read the last score.
An array’s length does not change after the array is created. Its elements can still be changed.
An array can also be empty, with length 0. It has no valid indexes. For an empty array, length - 1 is -1, so do not try to read its first or last element.
Check an index before using it
An index is valid only when both of these are true:
- It is at least 0.
- It is less than the array’s length.
In the program, index >= 0 && index < scores.length checks both limits. The && means that both tests must be true. With index set to 3, the second test is false, so the program prints a message instead of reading scores[3].
If you access an element outside the bounds, Java throws an ArrayIndexOutOfBoundsException. An exception is a signal that something went wrong while the program was running. For example, reading scores[3] in this program would cause that exception. Negative indexes are also invalid.
Practice
Exercise
For an array named values with length 4, which indexes are valid? Is values[4] valid?
Show answer
The valid indexes are 0, 1, 2, and 3. values[4] is not valid because the index must be less than the length.
Exercise
In the program, change index from 3 to 2. What does the final line print?
Show answer
It prints "At index: 100" because index 2 is valid and holds the last score.