Java's Iterator Interfaces - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Java's Iterator Interfaces

Description:

Java's Iterator Interfaces. Chapter 8. 2. Chapter Contents. The Interface Iterator ... import java.util. ... Java Class Library: ArrayList and LinkedList Revisited ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 22
Provided by: steve1789
Category:

less

Transcript and Presenter's Notes

Title: Java's Iterator Interfaces


1
Java's Iterator Interfaces
  • Chapter 8

2
Chapter Contents
  • The Interface Iterator
  • Implementing the Interface Iterator
  • A Linked Implementation
  • An Array-Based Implementation
  • The Interface ListIterator
  • Using the Interface ListIterator
  • An Array-Based Implementation of the Interface
    ListIterator
  • The Inner Class
  • Java Class Library ArrayList and LinkedList
    Revisited

3
The Interface Iterator
  • Methods provided
  • hasNext()Determines if current entry exists
  • next() Returns reference to current entry,
    advances iterator's marker to next entry
  • remove() Removes entry next has just returned

4
The Interface Iterator
Fig. 8-1 The effect of a call to next on a list
5
The Interface Iterator
Fig. 8-2 The effect of iterator methods on a list.
6
The Interface Iterator
Fig. 8-2 The effect of iterator methods on a list.
7
Implementing the Interface Iterator
  • We add an inner class to implementations of the
    ADT list
  • A linked implementation with hasNext, next
  • Array based list with all three methods of
    Iterator

import java.util.public interface
ListWithIteratorInterface extends
ListInterface public Iterator
getListIterator() // end ListWithIteratorInterf
ace
An interface that the list class can implement
8
A Linked Implementation
import java.util.NoSuchElementExceptionimport
java.lang.UnsupportedOperationException public
class LinkedListWithIterator implements
ListWithIteratorInterface private Node
firstNode private int length lt
Implementations of the constructor and methods of
the ADT list go here gt public Iterator
getListIterator() return new
IteratorForLinkedList() // end
getListIterator private class IteratorForLinkedLi
st implements Iterator private Node
currentNode // current node in
iteration public IteratorForLinkedList() cur
rentNode firstNode // end default
constructor lt Implementations of methods in the
interface Iterator go here. gt // end
IteratorForLinkedList lt Implementation of the
private class Node (Segment 6.22) goes here. gt
// end LinkedListWithIterator
9
An Array-Based Implementation
import java.util.public class
ArrayListWithIterator implements
ListWithIteratorInterface private Object
entry // array of list entries private int
length private static final int INITIAL_SIZE
25 lt Implementations of the constructor and
methods of the ADT list go heregt public
Iterator getListIterator() return new
IteratorForArrayList() // end
getListIterator private class IteratorForArrayLis
t implements Iterator lt Implementations of
methods in the interface Iterator go here. gt
// end IteratorForArrayList // end
ArrayListWithIterator
10
An Array-Based Implementation
Fig. 8-3 The array of list entries and current
Index (a) just before the call to next() (b)
just after the call to next() but before the call
to remove() (c) after the call to remove()
11
The Interface ListIterator
  • Methods provided
  • hasNext() Determines if current entry exists
  • next() Returns reference to current entry,
    advances iterator's marker to next entry
  • remove() Removes entry next has just returned
  • hasPrevious()Determines if there is another
    entry to visit
  • previous() Returns reference to current entry,
    moves iterator's marker back to previous entry

12
The Interface ListIterator
  • Methods provided
  • nextIndex(), previousIndex()Gets index of
    next/previous entry
  • add(Object newEntry) Adds an entry to list
    before entry that next() would have returned
  • set (Object newEntry)Replaces last entry in list
    that either next() or previous()has returned

13
The Interface ListIterator
Fig. 8-4 The effect of a call to previous() on
list.
14
The Interface ListIterator
Fig. 8-5 The indices returned by methods
nextIndex and previousIndex.
15
Array-Based ImplementationInterface ListIterator
  • Definition of an interface for list class
  • Includes operations of the ADT list
  • Returns instance of ListIterator instead of
    Iterator

import java.util.public interface
ListWithListIteratorInterface extends
ListInterface public ListIterator
getListIterator() // end ListWithListIteratorIn
terface
16
Array-Based ImplementationInterface ListIterator
import java.util.public class
ArrayListWithListIterator implements
ListWithListIteratorInterface private Object
entry // array of list entries private int
length // current number of entries in
list private static final int INITIAL_SIZE
25 lt Implementations of the constructor and
methods of the ADT list go here gt public
ListIterator getListIterator() return new
IteratorForArrayList() // end
getListIterator private class IteratorForArrayLis
t implements ListIterator lt Implementations
of the methods in ListIterator go here. gt //
end IteratorForArrayList // end
ArrayListWithListIterator
17
Array-Based ImplementationInterface ListIterator
Fig. 8-6 The array of list entries and
currentIndex (a) just before the call to add
(b) just after the call to add.
18
Array-Based ImplementationInterface ListIterator
Fig. 8-7 The array of list entries and
currentIndex (a) just before the call to
previous() (b) just after the call to previous()
but before the call to remove() (c) after the
call to remove()
19
Array-Based ImplementationInterface ListIterator
Fig. 8-8 a c Possible contexts in which the
method remove() is called
20
Array-Based ImplementationInterface ListIterator
(d)
(e)
Fig. 8-8 d e Possible contexts in which the
method remove() is called
21
Java Class Library ArrayList and LinkedList
Revisited
  • Both of these classes contain methods analogous
    to getListIterator
  • Adheres to Iterator interface
  • Begins at list element indicated by index
  • Where zero indicates first entry in the list

public Iterator iterator() public ListIterator
listIterator(int index)
Write a Comment
User Comments (0)
About PowerShow.com