Iterators - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Iterators

Description:

For simplicity, we will discuss the non-parameterized version of java.util.Iterator. ... throw new java.util.NoSuchElementException(); String temp ... – PowerPoint PPT presentation

Number of Views:119
Avg rating:3.0/5.0
Slides: 20
Provided by: georgejgr
Category:
Tags: iterators | java

less

Transcript and Presenter's Notes

Title: Iterators


1
Iterators
2
Iterator
  • An iterator is any object that allows one to step
    through each element in a list (or, more
    generally, some collection).

3
Iterator
  • Interface defined in java.util.Iterator
  • For simplicity, we will discuss the
    non-parameterized version of java.util.Iterator.
  • We will also implement our iterator as a public
    inner class of our linked list.

4
Iterator
  • Methods required by java.util.iterator
  • public void remove ( )
  • public Object next ( )
  • public boolean hasNext ( )

5
Iterator
  • public void remove ( )
  • optional
  • removes from the collection the last element
    returned by next
  • this method can be called only once per call to
    next()
  • if the collection is changed in any way, other
    than by using remove, the behavior of the
    iterator is not specified (and thus should be
    considered unpredictable)
  • throws IllegalStateException if the next() method
    has not yet been called, or if the remove()
    method has already been called after the last
    call to the next() method
  • throws UnsupportedOperationException if the
    remove operation is not supported by this
    iterator

6
Iterator
  • public Object next ( )
  • returns the next element of the collection that
    produced the iterator
  • throws a NoSuchElementException if there is no
    next element

7
Iterator
  • public boolean hasNext ( )
  • returns true if next() has not yet returned all
    the elements in the collection
  • returns false otherwise

8
Our iterator
  • public class MyListWithIterator
  • private class Node
  • String mData""
  • Node mNextnull
  • private Node ( String data )
  • mData data
  • //--------------------------------------------
    ---------------------------
  • public class Iterator implements
    java.util.Iterator
  • public void remove ( ) //optional
  • public Object next ( )
  • public boolean hasNext ( )
  • //--------------------------------------------
    ---------------------------

Inner class Node definition that we developed
previously.
9
Our iterator
  • public class MyListWithIterator
  • private class Node
  • String mData""
  • Node mNextnull
  • private Node ( String data )
  • mData data
  • //--------------------------------------------
    ---------------------------
  • public class Iterator implements
    java.util.Iterator
  • public void remove ( ) //optional
  • public Object next ( )
  • public boolean hasNext ( )
  • //--------------------------------------------
    ---------------------------

Inner class Iterator definition with interface
methods defined.
10
Our iterator
  • public class MyListWithIterator
  • private class Node
  • String mData""
  • Node mNextnull
  • private Node ( String data )
  • mData data
  • //--------------------------------------------
    ---------------------------
  • public class Iterator implements
    java.util.Iterator
  • public void remove ( ) //optional
  • public Object next ( )
  • public boolean hasNext ( )
  • //--------------------------------------------
    ---------------------------

The head of our linked list.
11
Our iterator
  • public class MyListWithIterator
  • private class Node
  • String mData""
  • Node mNextnull
  • private Node ( String data )
  • mData data
  • //--------------------------------------------
    ---------------------------
  • public class Iterator implements
    java.util.Iterator
  • public void remove ( ) //optional
  • public Object next ( )
  • public boolean hasNext ( )
  • //--------------------------------------------
    ---------------------------

A method to add elements to our linked list (as
before).
12
Our iterator
  • public class MyListWithIterator
  • private class Node
  • String mData""
  • Node mNextnull
  • private Node ( String data )
  • mData data
  • //--------------------------------------------
    ---------------------------
  • public class Iterator implements
    java.util.Iterator
  • public void remove ( ) //optional
  • public Object next ( )
  • public boolean hasNext ( )
  • //--------------------------------------------
    ---------------------------

New A public method that returns an iterator for
a specific linked list instance.
13
Our iterator
  • public class Iterator implements
    java.util.Iterator
  • private Node mNextElement mHead
  • //----------------------------------------
    ---------------------------
  • public void remove ( ) //optional
  • throw new UnsupportedOperationExceptio
    n()
  • //----------------------------------------
    ---------------------------
  • public Object next ( )
  • //----------------------------------------
    ---------------------------
  • public boolean hasNext ( )

We need something to keep track of where we are
as we iterate through the list.
14
Our iterator
  • public class Iterator implements
    java.util.Iterator
  • private Node mNextElement mHead
  • //----------------------------------------
    ---------------------------
  • //removes from the collection the last
    element returned by next.
  • //this method can be called only once per
    call to next(). If the
  • // collection is changed in any way,
    other than by using remove, the
  • // behavior of the iterator is not
    specified (and thus should be
  • // considered unpredictable).
  • //throws IllegalStateException if the
    next() method has not yet been
  • // called, or if the remove() method has
    already been called after
  • // the last call to the next() method.
  • //throws UnsupportedOperationException if
    the remove operation is not
  • // supported by this iterator.
  • public void remove ( ) //optional
  • throw new UnsupportedOperationExceptio
    n()
  • //----------------------------------------
    ---------------------------
  • public Object next ( )

Since remove() is optional, we will through the
indicated exception. It will be left as an
exercise to the reader.
15
Our iterator
Make a copy of the next string, move on to the
next item, and return the string. Privacy leak
possible?
  • public class Iterator implements
    java.util.Iterator
  • private Node mNextElement mHead
  • //----------------------------------------
    ---------------------------
  • public void remove ( ) //optional
  • throw new UnsupportedOperationExceptio
    n()
  • //----------------------------------------
    ---------------------------
  • //returns the next element of the
    collection that produced the
  • // iterator.
  • //throws a NoSuchElementException if
    there is no next element.
  • public Object next ( )
  • if (mNextElementnull)
  • throw new java.util.NoSuchElementE
    xception()
  • String temp mNextElement.mData
  • mNextElement mNextElement.mNext
  • return temp
  • //----------------------------------------
    ---------------------------
  • public boolean hasNext ( )

16
Our iterator
  • public class Iterator implements
    java.util.Iterator
  • private Node mNextElement mHead
  • public void remove ( ) //optional
  • throw new UnsupportedOperationExceptio
    n()
  • //----------------------------------------
    ---------------------------
  • public Object next ( )
  • if (mNextElementnull)
  • throw new java.util.NoSuchElementE
    xception()
  • String temp mNextElement.mData
  • mNextElement mNextElement.mNext
  • return temp
  • //----------------------------------------
    ---------------------------
  • //returns true if next() has not yet
    returned all the elements
  • // in the collection returns false
    otherwise.
  • public boolean hasNext ( )
  • return (mNextElement ! null)

When next is null, there is nothing left.
17
Using our iterator
  • public static void main ( String args )
  • MyListWithIterator list new
    MyListWithIterator()
  • list.add( "fred" )
  • list.add( "sally" )
  • MyListWithIterator.Iterator it
    list.iterator()
  • while (it.hasNext())
  • System.out.println( it.next() )

18
Our complete iterator
  • public class MyListWithIterator
  • private class Node
  • String mData""
  • Node mNextnull
  • private Node ( String data )
  • mData data
  • //--------------------------------------------
    ---------------------------
  • public class Iterator implements
    java.util.Iterator
  • private Node mNextElement mHead
  • //----------------------------------------
    ---------------------------
  • //removes from the collection the last
    element returned by next.
  • //this method can be called only once per
    call to next(). If the
  • // collection is changed in any way,
    other than by using remove, the
  • // behavior of the iterator is not
    specified (and thus should be
  • // considered unpredictable).
  • //throws IllegalStateException if the
    next() method has not yet been

This is too small to read but is provided in its
entirety so that you may cut and paste it into
your own code if you wish.
19
Our linked list and the for-each loop
  • To use our linked list class and our iterator
    with the for-each loop, we must first implement
    the java.util.Collection interface (along with
    the methods that this interface requires).
  • Then we may state
  • for (Object s list)
  • System.out.println( s )
  • Note Object s above. String s will cause the
    compiler to issue an error.
Write a Comment
User Comments (0)
About PowerShow.com