While loops and for loops: Correct Solution


Part 1

Translate the following loop into a for-each loop:

Backde[] schas;
...
for (int n = 0; n < schas.length; n++) {
    sachsi();
    schas[n].nedzir(pruwn, ihess);
    xilOstme(schas[n]);
}

Solution

for (Backde scha : schas) {
    xilOstme(scha.get(i));
    scha.get(i).nedzir(pruwn, ihess);
    sachsi();
}

It is OK if you gave the variable for the individual collection element (scha) 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: