Iterators - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Iterators

Description:

Iterators. Chapter 7. Chapter Contents. What is an Iterator? A Basic Iterator ... A Basic Iterator. Fig. 7-1 The effect of iterator methods on a list. ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 18
Provided by: steve1789
Category:
Tags: basic | iterators

less

Transcript and Presenter's Notes

Title: Iterators


1
Iterators
  • Chapter 7

2
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

3
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

4
A 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
5
A Basic Iterator
Fig. 7-1 The effect of iterator methods on a list.
6
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() / 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
7
Iterator Methods That Modify the ADT
Fig. 7-2 The effect of iterator methods on a
list.
8
Implementing 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
9
Implementing an Internal Iterator
Fig. 7-3 Before and after removing current entry
when its node is first in the chain (bottom of
p.159)
10
Implementing an Internal Iterator
Fig. 7-4 Before and after removing current entry
when its node is not first in the chain (top of
160)
11
Implementing an Internal Iterator
Fig. 7-5 Before and after adding an entry after
the current entry (bottom of p. 161)
12
Implementing an Iterator as Its Own Class
Fig. 7-6 Counting the number of times that Jane
appears in a list of names.
13
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

14
An 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
15
An 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).

16
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 at same time
  • Each can traverse list independently of one
    another

17
Inner Class Iterators
Fig. 7-8 An inner class iterator with direct
access to the linked chain that implements the
ADT.
Write a Comment
User Comments (0)
About PowerShow.com