CS62, Lecture 14

    Iterators

    Interface java.util.Iterator, part of the standard Java 5 libraries, specifies a way to cycle through all of the elements of a data structure. The interface java.util.Iterable specifies that a data structure supports an iterator.

    An example can be found with class ElementIterator, which is an iterator over NodePositionList.

    The interface java.util.ListIterator represents an iterator that moves both directions. It is supported by an iterator in class java.util.LinkedList. This iterator allows it to be used to represent a stack, queue, deque, or list. Note also the inheritance chains in the collection classes.

    We next discussed how to automatically keep a "favorites" list, by keeping track of the number of accesses of an element and moving it up the list each time it is accessed so that it is in order by number of accesses. The code can be found at