Files
unisa_strutture_dati_2013_2014/iterator/ElementIterator.java

35 lines
872 B
Java

package iterator;
import position.Position;
import position.PositionList;
import java.util.Iterator;
import java.util.NoSuchElementException;
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); }
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");
}
}