Presentation is loading. Please wait.

Presentation is loading. Please wait.

Iterator Summary Slides by Entesar Al-Mosallam adopted from Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.

Similar presentations


Presentation on theme: "Iterator Summary Slides by Entesar Al-Mosallam adopted from Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall."— Presentation transcript:

1 Iterator Summary Slides by Entesar Al-Mosallam adopted from Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

2 Iterator Interface --------------------- hasNext() Next() remove Separate Iterator class implements List Interface ListWithIteratorInterface ------------------------. getIterator() extends LinkedListWithIterator Class ArrayListWithIterator Class implements IteratorForLinkedList IteratorForArrayList implements

3 Interface Iterator + Class SeparateIterator public interface Iterator { public boolean hasNext (); public T next (); public void remove (); // Optional method } // end Iterator public class SeparateIterator implements Iterator { // implementation of public methods in interface Iterator } public class ClientTest { public static void main (... ) { ListInterface nameList = new LList (); Iterator nameIterator = new SeparateIterator (nameList); }

4 Inner Class Iterator public interface ListWithIteratorInterface extends ListInterface { public ListIterator getIterator (); } // end ListWithListIteratorInterface public class ArrayListWithIterator implements ListWithIteratorInterface { private T [] list; // array of list entries private int length; private static final int DEFAULT_INITIAL_CAPACITY = 25; // Constructors here // Implementations of the methods of the //ADT list go here; public Iterator getIterator () { return new IteratorForArrayList (); } // end getIterator private class IteratorForArrayList implements Iterator { //implementation of public methods of iterator interface } // end IteratorForArrayList } // end ArrayListWithIterator In Client Class: ListWithIteratorInterface nameList= new ArrayListWithIterator (); Iterator nameIterator = nameList.getIterator();

5 Inner Class Iterator public interface ListWithIteratorInterface extends ListInterface { public ListIterator getIterator (); } // end ListWithListIteratorInterface public class LinkedListWithIterator implements ListWithIteratorInterface { private Node firstNode; private int length; // Constructors here // Implementations of the methods of the // ADT list go here; public Iterator getIterator () { return new IteratorForArrayList (); } // end getIterator private class IteratorForLinkedList implements Iterator { //implementation of public methods of iterator interface } // end IteratorForLinkedList } // end LinkedListWithIterator In Client Class: ListWithIteratorInterface nameList= new LinkedListWithIterator (); Iterator nameIterator = nameList.getIterator();

6 interface ListIterator public interface ListIterator extends Iterator { public boolean hasNext (); public T next (); public void remove (); // Optional public boolean hasPrevious (); public T previous (); public int nextIndex (); public int previousIndex (); public void add (T newEntry); // Optional public void set (T newEntry); // Optional } // end ListIterator

7 Implementation of ListIterator Both next() and previous() set isRemoveOrSetLegal to true. add()sets isRemoveOrSetLegal to false, but does not need it to be true in order to add an item. In other words, it can add whether isRemoveOrSetLegal is true or false. remove() sets isRemoveOrSetLegal to false, and needs it to be true in order to remove an item. set() does not change the value of isRemoveOrSetLegal but needs it to be true to set an item.

8 Examples: Call condition Value of isRemoveOrSetLegal (after execution) Code Legal true false traverse.next() traverse.remove() traverse.add() Legal Illegal true false traverse.next() traverse.add() traverse.remove() Legal Illegal true false traverse.next() traverse.remove() traverse.set() Legal true false traverse.next() traverse.set() traverse.remove() Legal true false traverse.next() traverse.set() traverse.add() Legal Illegal true false traverse.next() traverse.add() traverse.set() Legal true false traverse.next() traverse.add()


Download ppt "Iterator Summary Slides by Entesar Al-Mosallam adopted from Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall."

Similar presentations


Ads by Google