Java's Iterator Interfaces - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Java's Iterator Interfaces

Description:

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

Number of Views:279
Avg rating:3.0/5.0
Slides: 20
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
  • 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 three methods are hasNext(), next() and
remove()
6
Implementing the Interface Iterator
  • Well demonstrate two examples
  • A linked list with exceptional remove()
  • Array based list with all three methods
  • Both examples implement the following

import java.util.public interface
ListWithIteratorInterface extends
ListInterface public Iterator
getListIterator()
An interface that the lists can implement
7
Impl 1 Linked List
import java.util.NoSuchElementExceptionimport
java.lang.UnsupportedOperationException public
class LinkedListWithIterator implements
ListWithIteratorInterface private Node
firstNode private int length lt 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 see p. 178 for hasNext(),
next() remove() gt lt private class Node
(Segment 6.22) goes here. gt
8
Impl 2 Array-Based List
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 constructor and methods of ADT list go
heregt public Iterator getListIterator() retur
n new IteratorForArrayList() private class
IteratorForArrayList implements Iterator lt
see pp. 180-1 for hasNext(), next() and remove()
gt
9
An Array-Based Implementation
(c) remove() removes Chris
(b) next() returns Chris
Bart
Bart
Deb
currentIndex
Deb
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()
10
The Interface ListIterator
  • Methods provided
  • Iterators big three
  • hasNext(),next(), remove()
  • hasPrevious()
  • previous() Returns current entry, moves
    iterator's marker back to previous entry
  • nextIndex(), previousIndex()Gets index of
    next/previous entry
  • add(Object newEntry) Inserts just before the
    next next() or just after the next previous()
  • set (Object newEntry)Replaces last call to
    previous() or next()

11
The Interface ListIterator
Fig. 8-4 The effect of a call to previous() on
list.
12
The Interface ListIterator
Fig. 8-5 The indices returned by methods
nextIndex and previousIndex.
13
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()
14
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 private
static final int INITIAL_SIZE 25 lt
constructor and methods of ADT list go here
gt public ListIterator getListIterator() return
new IteratorForArrayList() private class
IteratorForArrayList implements
ListIterator lt methods in ListIterator go
here. gt
15
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.
16
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()
17
Array-Based ImplementationInterface ListIterator
Fig. 8-8 a c Possible contexts in which the
method remove() is called
18
Array-Based ImplementationInterface ListIterator
(d)
(e)
Fig. 8-8 d e Possible contexts in which the
method remove() is called
19
Java Class Library ArrayList and LinkedList
Revisited
  • Both of these classes contain methods analogous
    to getIterator and getListIterator

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