While loops and for loops: Correct Solution


Part 1

Translate the following loop into a for-each loop:

List<Proca> pems;
...
for (int i = 0; i < pems.size(); i++) {
    aand(pems.get(i), 3);
    pems.get(i).adgio(0);
    gello();
}

Solution

for (Proca pem : pems) {
    gello();
    pem.get(i).adgio(0);
    aand(pem.get(i), 3);
}

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