Java Arena
Step 1 of 7

The for-each loop

The last module went through lists with an index: for (int i = 0; i < list.size(); i++) and list.get(i). When you just want every value, from the first to the last, Java has a shorter loop, the for-each loop:

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> planets = new ArrayList<>();
        planets.add("Mercury");
        planets.add("Venus");
        planets.add("Earth");

        for (String planet : planets) {
            System.out.println(planet + " orbits the Sun");
        }
    }
}
Output
Mercury orbits the Sun
Venus orbits the Sun
Earth orbits the Sun

Read for (String planet : planets) as "for each String planet in planets".

  • The loop runs once for each value in the list. In each round, planet holds the next value: first "Mercury", then "Venus", then "Earth".
  • The type before the name is the type of the list's values. For an ArrayList<Integer>, write for (int number : numbers): each Integer comes out as a plain int.
  • You pick the name. The singular of the list's name, like planet for planets, reads well.

The same loop with an index needs a counter, a condition, size() and get:

for (int i = 0; i < planets.size(); i++) {
    String planet = planets.get(i);
    System.out.println(planet + " orbits the Sun");
}

When the index loop is still the right tool

A for-each loop gives you the values, but not their places in the list. Use a loop with an index when:

  • you need the index itself, for example to print 1. Mercury, or to compare a value with the one next to it;
  • you go through the list in another order, such as backwards from the last value;
  • you remove values from the list while going through it (step 4 shows why).

For everything else, such as printing, adding up or counting, the for-each loop is shorter, and there's no index to get wrong.

A quick way to see a whole list at once: System.out.println(planets) prints [Mercury, Venus, Earth], the values in square brackets. It's handy for checking your work.

Your task

Challenge 1 of 3

Fill in the blanks, so the for-each loop prints every task on the list. The program prints:

- water the plants
- call the dentist
- buy stamps

Challenge 2

Read the ingredients of a recipe until the line end, adding each one to the list ingredients. Then use a for-each loop to print You need: and each ingredient, in order, and finally Happy cooking!. For the input flour, butter, two eggs and end the program prints:

You need: flour
You need: butter
You need: two eggs
Happy cooking!

Challenge 3

Things come out of a moving box in the opposite order to how they went in. Read items until the line close. Print Packed: and each item in order with a for-each loop. Then print Unpacked: and each item from the last to the first, with an index loop that counts down. For the input books, lamp, mugs the program prints:

Packed: books
Packed: lamp
Packed: mugs
Unpacked: mugs
Unpacked: lamp
Unpacked: books