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

Method Index

 o hasMoreElements()
Determine if the iteration is finished.
 o nextElement()
Return current value and increment Iterator.
 o reset()
Reset iterator to the beginning of the structure.
 o value()
Return structure's current object reference.

Methods

 o 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.
 o 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.
 o reset
public abstract void reset()
Reset iterator to the beginning of the structure.

Postcondition:
the iterator is reset to the beginning of the traversal

 o 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