Java - Loop differences ?
Go to solution
Solved by elpiop,
A for-each construct has an iterator 'under the hood' so to speak, however you are unable to remove objects from collections using such a loop. Since it hides the iterator, the foreach loop has the advantage of improved readability. If you instead write your own iterator, you are then able to modify the collection while you are iterating through it (this is not possible with the other loops).
While loops are useful when you need to run code until a certain condition is met.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now