160 likes | 286 Views
Tirgul OOP No.3. Iterators. What is an Iterator?. An object that provides a way to access elements of an aggregate object sequentially, without exposing it ’ s internal structure. Example. Suppose we have a myList object, which implements the List interface :
E N D
Tirgul OOP No.3 Iterators
What is an Iterator? • An object that provides a way to access elements of an aggregate object sequentially, without exposing it’s internal structure
Example • Suppose we have a myList object, which implements the List interface : public void remove(Object item); public void add(Object item); public Object elementAt(int index); • We would like to print all of the elements in the Cartesian product (myList X mylist)
Naive Solution - 1 • Add a function to the list interface which performs the task. • Why is that bad ? • What about myList X myList X myList ? • What if we change the list implementation ?
Naive Solution - 2 void printAllPairs(IntList list) { for(int i=0; i<list.size(); i++) { int e1 = list.elementAt(i); for( int j=0; j<list.size(); j++) { int e2 = list.elementAt(j); System.out.println (“<“+e1+”,”+e2+”>”); } } } Pros and cons?
Outline • Definition • Motivation • Basic Implementation and usage • Implementation issues
Declaring an Iterator • The Java iterator interface public class Iterator { public Iterator(); public boolean hasNext(); public Object next(); } • Create a ListIterator implementation for List • Add the following to the List interface: public Iterator iterator() …
ImplementationAssuming Linked List public class LinkedList . . . { . . . private class LinkedListIterator . . . { Node _position; . . . LinkedListIterator(Node head) { _position = head; } . . .
ImplementationAssuming Linked List public Object next() { . . . // Check validity Object ret = _position._data; _position = _position._next; return ret; } public boolean hasNext() { return _position != null; } public Iterator iterator() { return new LinkedListIterator(_head); } }
Using an Iterator void printAllPairs(List list) { Iterator i1; for (i1 = list.iterator(); i1.hasNext(); ){ Iterator i2; Object e1 = i1.next(); for (i2 = list.iterator(); i2.hasNext();){ Object e2 = i2.next(); System.out.println(“<“+e1+”,”+e2+”>”); } } }
Outline • Definition • Motivation • Basic Implementation and usage • Implementation issues: • Who defines the traversal algorithm? • Using auxiliary data structures • Access rights of an iterator • Iterators for composites
Who defines the traversal algorithm? Options : • The aggregate object • Next operation belongs to the aggregate object • The iterator merely points to the current position • The iterator • Better separation from the point of view of the client • Might require privileged access rights
Using an Auxiliary Data Structure public class List . . . { . . . public Iterator filtIterator(Object fltr){ List auxList = new List; Iterator i; for (i =iterator(); i.hasNext() ; ) { Object curItem = i.next(); if (!curItem.equals(fltr)) auxList.add(curItem) ; } return new FiltIterator(auxList); } . . . }
Iterator Defined Traversal - 1 public class List { . . . private class FilteredIterator . . . { Node _current; Object _filter; public FilteredIterator (Node head, Object filter) { _current = head; _filter = filter; advanceToNext(); } public boolean hasNext() { return (_current != null); } Note the access rights of the iterator
Iterator Defined Traversal - 2 public Object next() { advanceToNext(); . . . // Check validity return _current; } void advanceToNext() { boolean flag = true; while(flag && _current != null) if( _current.data.equals(_filter) ) _current = _current.next; else flag = false; } }
Designing Iterators for Trees Options: • Store a path to keep track of the current positions • Use an auxiliary list • Enhance the composite data structure • Add access to children, parents and siblings • Use an internal iterator