🧱 Arrays · Beginner

Iterating arrays in Java

Index loops vs for-each.

🧩 The mysteryTwo loops walk the same array. One doubles every element. The other "doubles" them and changes nothing at all. Can you tell which is which?

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]);
}
🔮 Predict it

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] + " ");
}
  1. 60 30
  2. 60 40 20
  3. 10 40
  4. 60 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.

⚠️ The trap

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}
🔮 Predict it

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]);
  1. 9
  2. 39
  3. 6
  4. 36
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.

🤔 Think first

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 the real world

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

  1. Index loop: positions, writes, reverse or stepped order
  2. for-each: read every value in order, no index
  3. Assigning to the for-each variable doesn't change the array
  4. Always use i < a.length, never i <= a.length
🤯 Did you know?

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);
  1. 6
  2. 12
  3. 246
  4. 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]);
}
  1. 54321
  2. 135
  3. 531
  4. 42
Check your answer

531. i starts at 4 and steps down by 2: indexes 4, 2, 0, which hold 5, 3 and 1.

Next: copy an array with b = a, change b... and a changes too. Spooky action at a distance?