Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › JAVA FUNDAMENTALS
The array values contains four elements: 6, -2, 9, and 4. Its length is therefore 4. Because indexing starts at 0, those four elements use indices 0, 1, 2, and 3. The number 4 describes how many elements exist, not the index of the last element.
int[] values = {6, -2, 9, 4};
System.out.println(values.length); // 4
System.out.println(values[3]); // 4
System.out.println(values[4]); // ArrayIndexOutOfBoundsExceptionThere is no element at values[4]. Java detects that the index is outside the array and throws ArrayIndexOutOfBoundsException when the access is attempted. A loop that treats values.length as an index will work through the real elements, then fail when it tries to read this nonexistent fifth position.
What is the final valid index of values = [6, -2, 9, 4]?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
An index-controlled loop starts i at 0 and reads values[i]. After each pass, i increases by 1. The condition i < values.length allows i values 0, 1, 2, and 3, then rejects i = 4 because 4 < 4 is false.
int[] values = {6, -2, 9, 4};
for (int i = 0; i < values.length; i++) {
System.out.println(values[i]);
}Changing the less-than sign to less-than-or-equal changes the final test. With i <= values.length, the loop still reads indices 0, 1, 2, and 3, but then it permits i = 4. The body attempts values[4], so the traversal ends with ArrayIndexOutOfBoundsException instead of finishing safely.
During each pass, i identifies a position and values[i] supplies the value at that position. For values = [6, -2, 9, 4], the loop creates four index-value pairs in order: 0 with 6, 1 with -2, 2 with 9, and 3 with 4.
| I | VALUES[I] | PRINTED LINE |
|---|---|---|
| 0 | 6 | 0: 6 |
| 1 | -2 | 1: -2 |
| 2 | 9 | 2: 9 |
| 3 | 4 | 3: 4 |
int[] values = {6, -2, 9, 4};
for (int i = 0; i < values.length; i++) {
System.out.println(i + ": " + values[i]);
}
// Output:
// 0: 6
// 1: -2
// 2: 9
// 3: 4The resulting print order is 0: 6, then 1: -2, then 2: 9, then 3: 4. The loop does not jump directly from one value to the next without state. Each iteration first has a particular index, and that index determines which array element values[i] reads.
Replace the incorrect condition so this loop traverses values without accessing values[4].
for (int i = 0; i <= values.length; i++) {
System.out.println(i + ": " + values[i]);
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
When the task needs only the values in array order, an enhanced for loop is shorter because Java supplies each value directly. For values, its four iterations receive 6, -2, 9, and 4 in that order. You do not write an index or access values[i] in the loop body.
int[] values = {6, -2, 9, 4};
for (int value : values) {
System.out.println(value);
}
// Output:
// 6
// -2
// 9
// 4This form fits value-only work, such as printing 6, -2, 9, and 4. It does not expose whether the current value came from index 0, 1, 2, or 3. If the position must be printed or used, choose the index-controlled form, because that form gives you i as well as values[i].