Iterator is guaranteed to have the following methods:
- hasNext()
- next()
- remove (optional)
When for-each
loop iterates through iterable
class objects, how does it know what object to start with? The above methods provide clear path forward, but what points to the starting element?
For-each
guarantees to iterate through all relevant objects. Depending on a class and its next()
implementation it could be vitally important where to start (consider forward linked list
or any other implementation that has a root
element).
How does it work?
If my question does not make sense please explain why.