Title: Tirgul OOP No.3
1Tirgul OOP No.3
2What is an Iterator?
- An object that provides a way to access elements
of an aggregate object sequentially, without
exposing its internal structure
3Example
- 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 i0
iltlist.size() i) int e1
list.elementAt(i) for( int j0
jltlist.size() j) int e2
list.elementAt(j) System.out.println
(lte1,e2gt)
Pros and cons?
6Outline
- Definition
- Motivation
- Basic Implementation and usage
- Implementation issues
7Declaring 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()
8ImplementationAssuming Linked List
public class LinkedList . . . . . . private
class LinkedListIterator . . . Node
_position . . . LinkedListIterator(Node
head) _position head . . .
9ImplementationAssuming 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)
10Using 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(lte1,e2gt)
11Outline
- 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
12Who 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
13Using 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) . . .
14Iterator 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
16Designing 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