Title: Iterators
1Iterators
2Chapter 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
3What 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
4A Basic Iterator
import java.util.NoSuchElementException public
interface BasicIteratorInterface /
Have we gone past last? / public boolean
hasCurrent() / Advance position by one
/ public boolean advance() / Retrieve
current entry / public Object getCurrent()
throws NoSuchElementException / Restart at
beginning / public void reset() // end
BasicIteratorInterface
5A Basic Iterator
Fig. 7-1 The effect of iterator methods on a list.
6Iterator 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() / Add a new entry
after current / public void addAfterCurrent(Obje
ct newEntry) throws NoSuchElementException /
remove current advance to next / public void
removeCurrent() throws NoSuchElementException /
if hasCurrent(), replace stay where we are.
/ public void replaceCurrent(Object
newEntry) throws NoSuchElementException //
end IteratorInterface
7Iterator Methods That Modify the ADT
Fig. 7-2 The effect of iterator methods on a
list.
8Implementing an Internal Iterator
- Including IteratorInterface methods
import java.util.NoSuchElementExceptionpublic
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 lt Other implementations go
heregt // end LinkedListWithInternalIterator
9Implementing an Internal Iterator
Fig. 7-3 Before and after removing current entry
when its node is first in the chain (bottom of
p.159)
10Implementing an Internal Iterator
Fig. 7-4 Before and after removing current entry
when its node is not first in the chain (top of
160)
11Implementing an Internal Iterator
Fig. 7-5 Before and after adding an entry after
the current entry (bottom of p. 161)
12Implementing an Iterator as Its Own Class
Fig. 7-6 Counting the number of times that Jane
appears in a list of names.
13Implementing 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
14An External Iterator
Fig. 7-7 An external iterator with a reference to
an ADT, an indicator of its position within the
iteration, and no knowledge of the ADT's
implementation
15An External Iterator
- An external iterator must access an ADT's data by
using public methods of the ADT - Therefore slower in performing its operations
- At the same time its implementation is
straightforward - Possible to have multiple instances of
independent external iterators - To provide a new type of iterator for an existing
implementation of an ADT that cannot be altered,
one must use an external iterator (p. 166 note).
16Inner 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 at same time
- Each can traverse list independently of one
another
17Inner Class Iterators
Fig. 7-8 An inner class iterator with direct
access to the linked chain that implements the
ADT.