Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection)."— Presentation transcript:

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.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.We will also implement our iterator as a public inner class of our linked list.

4 Iterator  Methods required by java.util.iterator: 1.public void remove ( ) 2.public Object next ( ) 3.public boolean hasNext ( )

5 Iterator  public void remove ( ) optionaloptional removes from the collection the last element returned by nextremoves from the collection the last element returned by next this method can be called only once per call to 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)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() methodthrows 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 iteratorthrows 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 iteratorreturns the next element of the collection that produced the iterator throws a NoSuchElementException if there is no next elementthrows 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 collectionreturns true if next() has not yet returned all the elements in the collection returns false otherwisereturns false otherwise

8 Our iterator public class MyListWithIterator { private class Node { private class Node { String mData=""; String mData=""; Node mNext=null; Node mNext=null; private Node ( String data ) { private Node ( String data ) { mData = data; mData = data; } } //----------------------------------------------------------------------- //----------------------------------------------------------------------- public class Iterator implements java.util.Iterator { public class Iterator implements java.util.Iterator { public void remove ( ) { //optional public void remove ( ) { //optional } public Object next ( ) { public Object next ( ) { } public boolean hasNext ( ) { public boolean hasNext ( ) { } } //----------------------------------------------------------------------- //----------------------------------------------------------------------- private Node mHead=null; private Node mHead=null; public void add ( String s ) { public void add ( String s ) { Node n = new Node( s ); Node n = new Node( s ); n.mNext = mHead; n.mNext = mHead; mHead = n; mHead = n; } public Iterator iterator ( ) { public Iterator iterator ( ) { return new Iterator(); return new Iterator(); }} Inner class Node definition that we developed previously.

9 Our iterator public class MyListWithIterator { private class Node { private class Node { String mData=""; String mData=""; Node mNext=null; Node mNext=null; private Node ( String data ) { private Node ( String data ) { mData = data; mData = data; } } //----------------------------------------------------------------------- //----------------------------------------------------------------------- public class Iterator implements java.util.Iterator { public class Iterator implements java.util.Iterator { public void remove ( ) { //optional public void remove ( ) { //optional } public Object next ( ) { public Object next ( ) { } public boolean hasNext ( ) { public boolean hasNext ( ) { } } //----------------------------------------------------------------------- //----------------------------------------------------------------------- private Node mHead=null; private Node mHead=null; public void add ( String s ) { public void add ( String s ) { Node n = new Node( s ); Node n = new Node( s ); n.mNext = mHead; n.mNext = mHead; mHead = n; mHead = n; } public Iterator iterator ( ) { public Iterator iterator ( ) { return new Iterator(); return new Iterator(); }} Inner class Iterator definition with interface methods defined.

10 Our iterator public class MyListWithIterator { private class Node { private class Node { String mData=""; String mData=""; Node mNext=null; Node mNext=null; private Node ( String data ) { private Node ( String data ) { mData = data; mData = data; } } //----------------------------------------------------------------------- //----------------------------------------------------------------------- public class Iterator implements java.util.Iterator { public class Iterator implements java.util.Iterator { public void remove ( ) { //optional public void remove ( ) { //optional } public Object next ( ) { public Object next ( ) { } public boolean hasNext ( ) { public boolean hasNext ( ) { } } //----------------------------------------------------------------------- //----------------------------------------------------------------------- private Node mHead=null; private Node mHead=null; public void add ( String s ) { public void add ( String s ) { Node n = new Node( s ); Node n = new Node( s ); n.mNext = mHead; n.mNext = mHead; mHead = n; mHead = n; } public Iterator iterator ( ) { public Iterator iterator ( ) { return new Iterator(); return new Iterator(); }} The head of our linked list.

11 Our iterator public class MyListWithIterator { private class Node { private class Node { String mData=""; String mData=""; Node mNext=null; Node mNext=null; private Node ( String data ) { private Node ( String data ) { mData = data; mData = data; } } //----------------------------------------------------------------------- //----------------------------------------------------------------------- public class Iterator implements java.util.Iterator { public class Iterator implements java.util.Iterator { public void remove ( ) { //optional public void remove ( ) { //optional } public Object next ( ) { public Object next ( ) { } public boolean hasNext ( ) { public boolean hasNext ( ) { } } //----------------------------------------------------------------------- //----------------------------------------------------------------------- private Node mHead=null; private Node mHead=null; public void add ( String s ) { public void add ( String s ) { Node n = new Node( s ); Node n = new Node( s ); n.mNext = mHead; n.mNext = mHead; mHead = n; mHead = n; } public Iterator iterator ( ) { public Iterator iterator ( ) { return new Iterator(); return new Iterator(); }} A method to add elements to our linked list (as before).

12 Our iterator public class MyListWithIterator { private class Node { private class Node { String mData=""; String mData=""; Node mNext=null; Node mNext=null; private Node ( String data ) { private Node ( String data ) { mData = data; mData = data; } } //----------------------------------------------------------------------- //----------------------------------------------------------------------- public class Iterator implements java.util.Iterator { public class Iterator implements java.util.Iterator { public void remove ( ) { //optional public void remove ( ) { //optional } public Object next ( ) { public Object next ( ) { } public boolean hasNext ( ) { public boolean hasNext ( ) { } } //----------------------------------------------------------------------- //----------------------------------------------------------------------- private Node mHead=null; private Node mHead=null; public void add ( String s ) { public void add ( String s ) { Node n = new Node( s ); Node n = new Node( s ); n.mNext = mHead; n.mNext = mHead; mHead = n; mHead = n; } public Iterator iterator ( ) { public Iterator iterator ( ) { return new Iterator(); return new Iterator(); }} New: A public method that returns an iterator for a specific linked list instance.

13 Our iterator public class Iterator implements java.util.Iterator { public class Iterator implements java.util.Iterator { private Node mNextElement = mHead; private Node mNextElement = mHead; //------------------------------------------------------------------- //------------------------------------------------------------------- public void remove ( ) { //optional public void remove ( ) { //optional throw new UnsupportedOperationException(); throw new UnsupportedOperationException(); } //------------------------------------------------------------------- //------------------------------------------------------------------- public Object next ( ) { public Object next ( ) { } //------------------------------------------------------------------- //------------------------------------------------------------------- public boolean hasNext ( ) { 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 { public class Iterator implements java.util.Iterator { private Node mNextElement = mHead; private Node mNextElement = mHead; //------------------------------------------------------------------- //------------------------------------------------------------------- //removes from the collection the last element returned by next. //removes from the collection the last element returned by next. //this method can be called only once per call to next(). If the //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 // collection is changed in any way, other than by using remove, the // behavior of the iterator is not specified (and thus should be // behavior of the iterator is not specified (and thus should be // considered unpredictable). // considered unpredictable). //throws IllegalStateException if the next() method has not yet been //throws IllegalStateException if the next() method has not yet been // called, or if the remove() method has already been called after // called, or if the remove() method has already been called after // the last call to the next() method. // the last call to the next() method. //throws UnsupportedOperationException if the remove operation is not //throws UnsupportedOperationException if the remove operation is not // supported by this iterator. // supported by this iterator. public void remove ( ) { //optional public void remove ( ) { //optional throw new UnsupportedOperationException(); throw new UnsupportedOperationException(); } //------------------------------------------------------------------- //------------------------------------------------------------------- public Object next ( ) { public Object next ( ) { } //------------------------------------------------------------------- //------------------------------------------------------------------- public boolean hasNext ( ) { public boolean hasNext ( ) { } } Since remove() is optional, we will through the indicated exception. It will be left as an exercise to the reader.

15 Our iterator public class Iterator implements java.util.Iterator { public class Iterator implements java.util.Iterator { private Node mNextElement = mHead; private Node mNextElement = mHead; //------------------------------------------------------------------- //------------------------------------------------------------------- public void remove ( ) { //optional public void remove ( ) { //optional throw new UnsupportedOperationException(); throw new UnsupportedOperationException(); } //------------------------------------------------------------------- //------------------------------------------------------------------- //returns the next element of the collection that produced the //returns the next element of the collection that produced the // iterator. // iterator. //throws a NoSuchElementException if there is no next element. //throws a NoSuchElementException if there is no next element. public Object next ( ) { public Object next ( ) { if (mNextElement==null) if (mNextElement==null) throw new java.util.NoSuchElementException(); throw new java.util.NoSuchElementException(); String temp = mNextElement.mData; String temp = mNextElement.mData; mNextElement = mNextElement.mNext; mNextElement = mNextElement.mNext; return temp; return temp; } //------------------------------------------------------------------- //------------------------------------------------------------------- public boolean hasNext ( ) { public boolean hasNext ( ) { } } Make a copy of the next string, move on to the next item, and return the string. Privacy leak possible?

16 Our iterator public class Iterator implements java.util.Iterator { public class Iterator implements java.util.Iterator { private Node mNextElement = mHead; private Node mNextElement = mHead; public void remove ( ) { //optional public void remove ( ) { //optional throw new UnsupportedOperationException(); throw new UnsupportedOperationException(); } //------------------------------------------------------------------- //------------------------------------------------------------------- public Object next ( ) { public Object next ( ) { if (mNextElement==null) if (mNextElement==null) throw new java.util.NoSuchElementException(); throw new java.util.NoSuchElementException(); String temp = mNextElement.mData; String temp = mNextElement.mData; mNextElement = mNextElement.mNext; mNextElement = mNextElement.mNext; return temp; return temp; } //------------------------------------------------------------------- //------------------------------------------------------------------- //returns true if next() has not yet returned all the elements //returns true if next() has not yet returned all the elements // in the collection; returns false otherwise. // in the collection; returns false otherwise. public boolean hasNext ( ) { public boolean hasNext ( ) { return (mNextElement != null); return (mNextElement != null); } } When next is null, there is nothing left.

17 Using our iterator public static void main ( String args[] ) { public static void main ( String args[] ) { MyListWithIterator list = new MyListWithIterator(); MyListWithIterator list = new MyListWithIterator(); list.add( "fred" ); list.add( "fred" ); list.add( "sally" ); list.add( "sally" ); MyListWithIterator.Iterator it = list.iterator(); MyListWithIterator.Iterator it = list.iterator(); while (it.hasNext()) { while (it.hasNext()) { System.out.println( it.next() ); System.out.println( it.next() ); } }

18 Our complete iterator public class MyListWithIterator { private class Node { private class Node { String mData=""; String mData=""; Node mNext=null; Node mNext=null; private Node ( String data ) { private Node ( String data ) { mData = data; mData = data; } } //----------------------------------------------------------------------- //----------------------------------------------------------------------- public class Iterator implements java.util.Iterator { public class Iterator implements java.util.Iterator { private Node mNextElement = mHead; private Node mNextElement = mHead; //------------------------------------------------------------------- //------------------------------------------------------------------- //removes from the collection the last element returned by next. //removes from the collection the last element returned by next. //this method can be called only once per call to next(). If the //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 // collection is changed in any way, other than by using remove, the // behavior of the iterator is not specified (and thus should be // behavior of the iterator is not specified (and thus should be // considered unpredictable). // considered unpredictable). //throws IllegalStateException if the next() method has not yet been //throws IllegalStateException if the next() method has not yet been // called, or if the remove() method has already been called after // called, or if the remove() method has already been called after // the last call to the next() method. // the last call to the next() method. //throws UnsupportedOperationException if the remove operation is not //throws UnsupportedOperationException if the remove operation is not // supported by this iterator. // supported by this iterator. public void remove ( ) { //optional public void remove ( ) { //optional throw new UnsupportedOperationException(); throw new UnsupportedOperationException(); } //------------------------------------------------------------------- //------------------------------------------------------------------- //returns the next element of the collection that produced the //returns the next element of the collection that produced the // iterator. // iterator. //throws a NoSuchElementException if there is no next element. //throws a NoSuchElementException if there is no next element. public Object next ( ) { public Object next ( ) { if (mNextElement==null) if (mNextElement==null) throw new java.util.NoSuchElementException(); throw new java.util.NoSuchElementException(); String temp = mNextElement.mData; String temp = mNextElement.mData; mNextElement = mNextElement.mNext; mNextElement = mNextElement.mNext; return temp; return temp; } //------------------------------------------------------------------- //------------------------------------------------------------------- //returns true if next() has not yet returned all the elements //returns true if next() has not yet returned all the elements // in the collection; returns false otherwise. // in the collection; returns false otherwise. public boolean hasNext ( ) { public boolean hasNext ( ) { return (mNextElement != null); return (mNextElement != null); } } //----------------------------------------------------------------------- //----------------------------------------------------------------------- private Node mHead=null; private Node mHead=null; //----------------------------------------------------------------------- //----------------------------------------------------------------------- public void add ( String s ) { public void add ( String s ) { Node n = new Node( s ); Node n = new Node( s ); n.mNext = mHead; n.mNext = mHead; mHead = n; mHead = n; } //----------------------------------------------------------------------- //----------------------------------------------------------------------- public Iterator iterator ( ) { public Iterator iterator ( ) { return new Iterator(); return new Iterator(); } //----------------------------------------------------------------------- //----------------------------------------------------------------------- public static void main ( String args[] ) { public static void main ( String args[] ) { MyListWithIterator list = new MyListWithIterator(); MyListWithIterator list = new MyListWithIterator(); list.add( "fred" ); list.add( "fred" ); list.add( "sally" ); list.add( "sally" ); MyListWithIterator.Iterator it = list.iterator(); MyListWithIterator.Iterator it = list.iterator(); while (it.hasNext()) { while (it.hasNext()) { System.out.println( it.next() ); System.out.println( it.next() ); } }} 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 ); System.out.println( s ); Note Object s above. String s will cause the compiler to issue an error.Note Object s above. String s will cause the compiler to issue an error.


Download ppt "Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection)."

Similar presentations


Ads by Google