Totals, flags and joined text
In the last module you added up a list with an index loop. With a for-each loop it's shorter, because there's no index to keep track of:
ArrayList<Integer> pages = new ArrayList<>();
pages.add(12);
pages.add(30);
pages.add(18);
int total = 0;
for (int count : pages) {
total += count;
}
System.out.println("Pages read: " + total);
System.out.println("Per day: " + (double) total / pages.size());
Pages read: 60
Per day: 20.0Averages and counts work just as before; only the loop is new. This step adds two more things a loop over a list often has to work out: a yes-or-no answer about all the values, and one line of text made from them.
Does every value pass? A flag
Some questions are about the whole list: is every value valid? Is any value negative? A boolean variable answers them. It's called a flag: it starts with the answer that holds until some value proves otherwise, and the loop changes it when a value does.
ArrayList<Integer> ages = new ArrayList<>();
ages.add(19);
ages.add(23);
ages.add(17);
ages.add(30);
boolean allAdults = true;
for (int age : ages) {
if (age < 18) {
allAdults = false;
}
}
if (allAdults) {
System.out.println("Everyone is 18 or over");
} else {
System.out.println("Someone is under 18");
}
Someone is under 18One age under 18 is enough to make the flag false, and nothing sets it back to true. That's why the if in the loop has no else: with else { allAdults = true; }, only the last age would decide, and here the 30 would hide the 17.
For "is there any ...?", turn it around: start the flag at false, and set it to true when a value matches.
Joining values into one line
Text can be built in a loop, as in module 11: start from "" and add each value with +=. To put a separator between the values, but not in front of the first one, check whether the text is still empty:
ArrayList<String> stops = new ArrayList<>();
stops.add("Home");
stops.add("Park");
stops.add("School");
String route = "";
for (String stop : stops) {
if (!route.equals("")) {
route += " > ";
}
route += stop;
}
System.out.println(route);
Home > Park > SchoolIn the first round route is still empty, so no separator goes in. From then on, each stop gets one in front of it.