1 / 16

Tirgul OOP No.3

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 :

leon
Download Presentation

Tirgul OOP No.3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Tirgul OOP No.3 Iterators

  2. What is an Iterator? • An object that provides a way to access elements of an aggregate object sequentially, without exposing it’s internal structure

  3. 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)

  4. 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 ?

  5. 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?

  6. Outline • Definition • Motivation • Basic Implementation and usage • Implementation issues

  7. 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() …

  8. ImplementationAssuming Linked List public class LinkedList . . . { . . . private class LinkedListIterator . . . { Node _position; . . . LinkedListIterator(Node head) { _position = head; } . . .

  9. 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); } }

  10. 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+”>”); } } }

  11. 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

  12. 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

  13. 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); } . . . }

  14. 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

  15. 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; } }

  16. 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

More Related