Professional Documents
Culture Documents
(Iterators)
Vincenzo Gulisano
Iterators
AED: Iterators
Iterators
Why iterators?
Iterator
Collection S
Current element
Next element
List
Current
Element
3
Next
Element
AED: Iterators
Iterators ADT
Iterator ADT
2 methods
AED: Iterators
AED: Iterators
AED: Iterators
Iterators
How to get an iterator?
List
Iterator
AED: Iterators
Iterators
java.lang.Iterable (interface)
AED: Iterators
AED: Iterators
Iterators
How to implement iterators?
First idea : Copy the elements into a queue
Queue
Collection
10
hasNext() = !isEmpty()
next() = deque()
AED: Iterators
Iterators
Using a queue
Better solution:
11
AED: Iterators
Iterators
Complications
Current
Element
remove
last element
Current
Element
next
12
Exception!
AED: Iterators
Iterators
Java API
13
AED: Iterators
Iterators
Current Next
Element Element
Current
Element
Next Element?
14
AED: Iterators
Iterators
15
AED: Iterators
Iterators
List<int> values;
AED: Iterators
Iterators
List iterators
Iterator
List Iterator
Current Next
Element Element
17
AED: Iterators
Iterators
18
AED: Iterators
Iterators
add(E o)
hasNext()
hasPrevious()
next()
previous()
nextIndex()
previousIndex()
remove()
set()
19
AED: Iterators
Iterators
next()
previous()
Prev Current Next
Element Element Element
hasNext()
hasPrevious()
True
20
Current
Element
AED: Iterators
True
Iterators
nextIndex()
previousIndex()
Prev Current
Index Element
21
AED: Iterators
Next
Index
Iterators
remove()
on previous() or next()
Current
Element
set()
22
on previous() or next()
AED: Iterators
Example
NodePositionList<E>
package net.datastructures;
import java.util.Iterator;
public class NodePositionList<E> implements PositionList<E> {
23
AED: Iterators
Example
NodePositionList<E>
implements
PositionList<E>
interface
extends
NodePositionList<String> x;
Iterator<E> i = x.iterator();
ElementIterator<E>
implements
Iterator<E>
interface
i.hasNext(); / i.next();
24
AED: Iterators
Iterable<E>
interface
Example - ElementIterator
public class ElementIterator<E> implements Iterator<E> {
protected PositionList<E> list; // the underlying list
protected Position<E> cursor; // the next position
public ElementIterator(PositionList<E> L) {
list = L;
cursor = (list.isEmpty()) ? null : list.first();
}
public boolean hasNext() {
return (cursor != null);
}
25
AED: Iterators
Example - ElementIterator
public E next() throws NoSuchElementException {
if (cursor == null)
throw new NoSuchElementException("No next element");
E toReturn = cursor.element();
cursor = (cursor == list.last()) ? null : list.next(cursor);
return toReturn;
}
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException("remove");
}
}
26
AED: Iterators
List Iterator
Print content:
27
<-0-> A
A <-1-> B
B <-2-> C
C <-3-> D
D <-4-> E
E <-5->
AED: Iterators
AED: Iterators
AED: Iterators
AED: Iterators
<-0-> A
A <-1-> A
A <-2-> A
A <-10000000-> A
AED: Iterators
AED: Iterators
AED: Iterators
34
AED: Iterators
AED: Iterators
<-0-> A
A <-1-> B
B <-2-> C
C <-3-> D
D <-4-> E
AED: Iterators
<-0-> A
A <-1-> B
B <-2-> C
C <-3-> D
D <-4-> E
E <-5->
}
System.out.println( myListIterator.previous() + " <-" + currentIndex + "-> ");
37
AED: Iterators
38
Exception
AED: Iterators
<-0-> A
A <-1-> B
B <-2-> C
C <-3-> D
D <-4-> E
E <-5->
}
if (myListIterator.hasPrevious())
{
System.out.println( myListIterator.previous() + " <-" + currentIndex + "-> ");
}
39
AED: Iterators