I just learned about how the Java Collections Framework implements data structures in linked lists. From what I understand, Iterators
are a way of traversing through the items in a data structure such as a list. Why is this interface used? Why are the methods hasNext()
, next()
and remove()
not directly coded to the data structure implementation itself?
From the Java website: link text
public interface Iterator<E>
An
iterator over a collection. Iterator
takes the place of Enumeration in the
Java collections framework. Iterators
differ from enumerations in two ways:
- Iterators allow the caller to remove
elements from the underlying
collection during the iteration with
well-defined semantics.
- Method names
have been improved.
This interface is
a member of the Java Collections
Framework.
I tried googling around and can't seem to find a definite answer. Can someone shed some light on why Sun chose to use them? Is it because of better design? Increased security? Good OO practice?
Any help will be greatly appreciated. Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…