All Packages Class Hierarchy This Package Previous Next Index
Interface structure.Iterator
- public interface Iterator
- extends Enumeration
The interface describing iterators. An iterator is an enumeration
whose traversal order can be guaranteed in some way. In addition
the iterator provides a reset primitive that allows the iterator
to be restarted without reallocation. A value primitive also
provides access to the current element without incrementing the iterator.
Iterators are the result of calling the elements, keys, edges, or
neighbors methods of various structures in the structure package.
Since iterators are independent objects, multiple iterators may be
constructed to traverse a single object.
Because iterators provide access to data within a structure, it is important
not to modify or destroy data that you get access to from the value
or nextElement methods. The state of these values may determine how
they are found in the underlying structure. Their modification could
cause the associated structure to become corrupt.
Typical use:
List l = new SinglyLinkedList();
// ...list gets built up...
Iterator li = l.elements();
while (li.hasMoreElements())
{
System.out.println(li.value());
li.nextElement();
}
li.reset();
while (li.hasMoreElements())
{ .... }
Iterators extend Java's java.util.Enumeration interface,
so the more traditional
use of Enumerations is also possible.
- See Also:
- java.util.Enumeration
hasMoreElements()
- Determine if the iteration is finished.
nextElement()
- Return current value and increment Iterator.
reset()
- Reset iterator to the beginning of the structure.
value()
- Return structure's current object reference.
hasMoreElements
public abstract boolean hasMoreElements()
- Determine if the iteration is finished.
- Postcondition:
- returns true if there is more structure to be viewed:
ie, if value (nextElement) can return a useful value.
- Returns:
- True if the iterator has more elements to be considered.
nextElement
public abstract Object nextElement()
- Return current value and increment Iterator.
- Precondition:
- traversal has more elements
- Postcondition:
- returns current value and increments the iterator
- Returns:
- The current value, before increment.
reset
public abstract void reset()
- Reset iterator to the beginning of the structure.
- Postcondition:
- the iterator is reset to the beginning of the traversal
value
public abstract Object value()
- Return structure's current object reference.
- Precondition:
- traversal has more elements
- Postcondition:
- returns the current value referenced by the iterator
- Returns:
- Object currently referenced.
All Packages Class Hierarchy This Package Previous Next Index