While loops and for loops: Correct Solution


Part 1

Translate the following loop into a for-each loop:

List<Susis> nels;
...
for (int i = 0; i < nels.size(); i++) {
    ictSedosm(sopior);
    sosm();
    nels.get(i).eesSerpan();
    enhad(warko, nels.get(i));
}

Solution

for (Susis nel : nels) {
    enhad(warko, nel.get(i));
    nel.get(i).eesSerpan();
    sosm();
    ictSedosm(sopior);
}

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