While loops and for loops: Correct Solution


Part 1

Translate the following loop into a for-each loop:

List<Ouless> terts;
...
for (int n = 0; n < terts.size(); n++) {
    whapru(terts.get(n));
    terts.get(n).naske(hiic);
}

Solution

for (Ouless tert : terts) {
    tert.get(i).naske(hiic);
    whapru(tert.get(i));
}

It is OK if you gave the variable for the individual collection element (tert) a different name, such as elem. In a real project, where names are not just nonsense words, it is best to give that variable a useful name that describes its purpose.


Related puzzles: