Iterator (cont’d)
Consequences:
- algorithms can be implemented independent of representation
- encapsulation is preserved
Implementation:
- who controls traversal
- internal vs. external iterators
- robust iterators: what happens when the aggregate is modified while iterator is active
- iterator is adjusted
- avoid it by iterating over a copy (expensive)
Iterators in java.util
- Iterator protocol is define by Enumeration interface
- Factory Method elements() returns a container’s iterator
- Iterator implementation can be package private
- Java iterators are not robust
public interface Enumeration {
boolean hasMoreElements();