Вы находитесь на странице: 1из 18

Iterators

Chapter 7
Chapter Contents
What is an Iterator?
• A Basic Iterator
• Iterator Methods that Modify the ADT

Implementing an Internal Iterator


Implementing an Iterator as Its Own Class
• An External Iterator
• An Inner Class
What Is an Iterator?
A program component
• Enables you to step through, traverse a
collection of data
• Can tell whether next entry exists

Iterator may be manipulated


• Asked to advance to next entry
• Give a reference to current entry
• Modify the list as you traverse it
A Basic Iterator
import java.util.NoSuchElementException;
public interface BasicIteratorInterface
{ /** Task: Determines whether the iteration has completed its traversal
Our
Ourmethods
methodsthrow
throw
* and gone beyond the last entry in the collection of data.
exceptions,
exceptions,must
mustimport
import
* @return true if the iteration has another entry to return */
exception fromjava.util
exceptionfrom java.util
public boolean hasCurrent();
/** Task: Advances the current position of the iteration by 1.
* @return true if the iteration has another entry to return */
public boolean advance();
/** Task: Retrieves the current entry in the iteration.
* @return a reference to the current entry in the iteration, if one exists
* @throws NoSuchElementException, if no current entry exists */
public Object getCurrent() throws NoSuchElementException;
/** Task: Sets the iteration to begin with the first entry in the collection. */
public void reset();
} // end BasicIteratorInterface
A Basic Iterator

Fig. 7-1 The effect of


iterator methods on a list.
Iterator Methods That Modify the ADT
import java.util.NoSuchElementException;
public interface IteratorInterface extends BasicIteratorInterface
{ public boolean hasCurrent();
public boolean advance();
public Object getCurrent() throws NoSuchElementException;
public void reset();
/** Task: Adds a new entry immediately after the current entry, if one exists.
* @param newEntry the object that is the new entry
* @throws NoSuchElementException, if no current entry exists */
public void addAfterCurrent(Object newEntry)
throws NoSuchElementException;
/** Task: Removes the current entry, if one exists, and advances the iteration to the next entry.
* @throws NoSuchElementException, if no current entry exists */
public void removeCurrent() throws NoSuchElementException;
/** Task: Replaces the current entry with a new entry.
* @param newEntry the object that replaces the current entry
* @throws NoSuchElementException, if no current entry exists */
public void replaceCurrent(Object newEntry)
throws NoSuchElementException;
} // end IteratorInterface
Iterator Methods That Modify the ADT

Fig. 7-2 The effect of


iterator methods on a list.
Iterator Methods That Modify the ADT

Fig. 7-2 (ctd.) The effect of iterator methods on a list.


Implementing an Internal Iterator
Including the methods that
IteratorInterface specifies
import java.util.NoSuchElementException;
public class LinkedListWithInternalIterator implements ListInterface, IteratorInterface
{ private Node firstNode;
private int length;
private Node currentNode; // current node in iteration
private Node priorNode; // node before the current node
public LinkedListWithInternalIterator()
{ clear(); } // end default constructor
public final void clear()
{ firstNode = null;
length = 0;
currentNode = null;
priorNode = null;
} // end clear
< Other implementations go here>
} // end LinkedListWithInternalIterator
Implementing an Internal Iterator

Fig. 7-3 Before and after removing current entry


when its node is first in the chain.
Implementing an Internal Iterator

Fig. 7-4 Before and after removing current entry


when its node is not first in the chain.
Implementing an Internal Iterator

Fig. 7-5 Before and after adding an


entry after the current entry.
Implementing an Iterator as Its Own
Class

Fig. 7-6 Counting the


number of times that
Jane appears in a list
of names.
Implementing an Iterator as Its
Own Class

Internal iterator easy to understand, use


• But only one iteration of a list can occur at any
one time
• May need multiple iterators for a single list

Create an external iterator


• Allows multiple instances of an iterator for a list
An External Iterator

Fig. 7-7 An external iterator with a reference to an ADT, an


indicator of its position within the iteration, and know
knowledge of the ADT's implementation
An External Iterator
An external iterator must access an ADT's
data by using public methods of the ADT
• As a result is slower in performing its
operations
• At the same time its implementation is
straightforward
Possible to have multiple instances of
independent external iterators
An iterator for an existing implementation of
an ADT that cannot be altered must be an
external iterator
Inner Class Iterators
Have direct access to an ADT's data
• Thus is as efficient as an internal iterator
• Has similar implementation effort

Advantage
• Can have several iterator objects in existence
at same time
• Each can traverse list independently of one
another
Inner Class Iterators

Fig. 7-8 An inner class iterator with direct access to the


linked chain that implements the ADT.

Вам также может понравиться