Iterating arrays in Java
Index loops vs for-each.
Two walkers
An index loop counts positions and reaches each element through a[i]. A for-each loop hands you each value in turn, from first to last, with no index.
for (int i = 0; i < a.length; i++) {
a[i] = a[i] * 2; // needs the index
}
for (int x : a) {
System.out.println(x); // just reading
}Pick by need
Use an index loop when you need positions, want to change elements, or walk in a different order (backwards, every 2nd...). Use for-each when you just need every value, first to last: it's shorter and can't go out of bounds. Either way: i < a.length, never <=.
// index loop: position + value
for (int i = 0; i < a.length; i++) {
System.out.println(i + ": " + a[i]);
}Stepping backwards
What prints?
int[] a = {10, 20, 30, 40, 50, 60};
for (int i = a.length - 1; i >= 0; i -= 3) {
System.out.print(a[i] + " ");
}60 3060 40 2010 4060 50 40
Show the answer
i starts at the last index (5) and steps down by 3: indexes 5 and 2, holding 60 and 30. The next value, -1, fails i >= 0.
for-each can't write
The for-each variable is a copy of each value. Assigning to it doesn't touch the array. To set every element (say, to 0), use an index loop or Arrays.fill(a, 0).
int[] a = {5, 6, 7};
for (int x : a) {
x = 0; // only the copy changes
}
// a is still {5, 6, 7}Which loop counts?
What prints?
int[] a = {1, 2, 3};
for (int x : a) {
x += 10;
}
for (int i = 0; i < a.length; i++) {
a[i] += 1;
}
System.out.println(a[0] + a[1] + a[2]);939636
Show the answer
The for-each adds 10 to copies only, so nothing changes. The index loop adds 1 to the real elements: 2, 3, 4. Their sum is 9.
Choose the loop
You need to print each fruit with its position, like 0: apple. Which loop?
Think about it, then reveal the answer
The index loop. It gives you i directly, so you print both i and a[i]. for-each hides the position; you'd need an extra counter variable to fake it.
In real projects
Professional code uses for-each by default because it's harder to get wrong, and reaches for index loops for in-place changes: brightening every pixel of an image, normalizing scores, or comparing each element with its neighbor.
Key takeaways
- Index loop: positions, writes, reverse or stepped order
- for-each: read every value in order, no index
- Assigning to the for-each variable doesn't change the array
- Always use i < a.length, never i <= a.length
The Java Language Specification literally defines a for-each over an array as an ordinary index loop with a hidden counter. Same speed, fewer chances to slip.
Practice questions
What does this print?
int[] a = {2, 4, 6};
int total = 0;
for (int x : a) {
total += x;
}
System.out.println(total);- 6
- 12
- 246
- 0
Check your answer
12. for-each visits 2, 4 and 6, adding each to total: 12.
What does this print?
int[] a = {1, 2, 3, 4, 5};
for (int i = a.length - 1; i >= 0; i -= 2) {
System.out.print(a[i]);
}- 54321
- 135
- 531
- 42
Check your answer
531. i starts at 4 and steps down by 2: indexes 4, 2, 0, which hold 5, 3 and 1.