Iterator is super class of ListIterator.
Here are the differences between them:
- With
iterator
you can move only forward, but with ListIterator
you can move backword also while reading the elements.
- With
ListIterator
you can obtain the index at any point while traversing, which is not possible with iterator
s.
- With
iterator
you can check only for next element available or not, but in listiterator
you can check previous and next elements.
- With
listiterator
you can add new element at any point of time, while traversing. Not possible with iterator
.
- With
listiterator
you can modify an element while traversing, which is not possible with iterator
.
Iterator look and feel:
public interface Iterator<E> {
boolean hasNext();
E next();
void remove(); //optional-->use only once with next(),
dont use it when u use for:each
}
ListIterator look and feel:
public interface ListIterator<E> extends Iterator<E> {
boolean hasNext();
E next();
boolean hasPrevious();
E previous();
int nextIndex();
int previousIndex();
void remove(); //optional
void set(E e); //optional
void add(E e); //optional
}