Iterator
Interface Diagram

Iterator
Interface Specification
Invariant
An Iterator object...
- is used to treat a collection (theCollection) like a linear list of items.
- position maintains a desginated position within theCollection
- 0 <= position <= theCollection.size()
(position == 0 denotes the front of the list and position == size() at the rear)
Update Method
public void remove ()
pre: isRemovable
post(expected):
theCollection == theCollection@pre with the positionth@pre item removed (i.e., the item most recently returned by next() is the one removed)
and theCollection.size() == theCollection.size()@pre - 1
and position == position@pre - 1
(i.e., the iterator is effectively unchanged in position)
and isRemovable == false
note: This
method must be supplied by the implementing class.
Query
Methods
public boolean hasNext( )
post: result == ( position != theCollection.size() )
(i.e. result is false exactly when iterator is positioned at the rear of
theCollection)
note: This
method must be supplied by the implementing class.
public ItemType next ()
pre: hasNext()
post: position == position@pre + 1
(i.e. the iterator's position advances by one item)
and result == the positionth item
and isRemovable == true
note: This
method must be supplied by the implementing class.