for (int i = 0; i < 10; i++) {
    // ...
    if (iWantToRepeat) {
        i--;
        continue;
    }
    // ...
}
for (Foo f : Bar) {
    // ...
    if (iWantToRepeat) {
        // What can I put here?
    }
    // ...
}
Is there any way to repeat an iteration of an enhanced for-loop? I get the feeling there might be because it's based on iterators and if I had access to them I could do it I think.
No, you can't. In every iteration the Iterator procedes by 1 step. However you can use a do-while loop to get the same effect:
for (Foo f : Bar) {
    boolean iWantToRepeat;
    do {
        // ...
        iWantToRepeat = //...;
        // ...
    } while(iWantToRepeat);
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With