Title: Iterators
1Iterators
2Iterator
- An iterator is any object that allows one to step
through each element in a list (or, more
generally, some collection).
3Iterator
- 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.
4Iterator
- Methods required by java.util.iterator
- public void remove ( )
- public Object next ( )
- public boolean hasNext ( )
5Iterator
- 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
6Iterator
- public Object next ( )
- returns the next element of the collection that
produced the iterator - throws a NoSuchElementException if there is no
next element
7Iterator
- public boolean hasNext ( )
- returns true if next() has not yet returned all
the elements in the collection - returns false otherwise
8Our 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.
9Our 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.
10Our 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.
11Our 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).
12Our 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.
13Our 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.
14Our 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.
15Our 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 ( )
16Our 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.
17Using 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() )
-
-
18Our 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.
19Our 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.