Possible Duplicate:
difference between Iterator and Listiterator?
Recently,while I was goint through javadocs, I found two methods in List interface : iterator() and listIterator(). Apart from different return type, what are the other differences between these two methods?
Below is the java doc for both method.
// List Iterators
/**
* Returns a list iterator over the elements in this list (in proper
* sequence).
*
* @return a list iterator over the elements in this list (in proper
* sequence)
*/
ListIterator<E> listIterator();
And
/**
* Returns an iterator over the elements in this list in proper sequence.
*
* @return an iterator over the elements in this list in proper sequence
*/
Iterator<E> iterator();
ListIterator is a subclass which extends Iterator.
A ListIterator allows traversal in both directions, rather than just checking if there are more elements (
hasNext()), and getting the next one (next()). It maintains a cursor position and calls tonext()andprevious()will alter the position and return relevant values. ListIterator also allows the addition (add(E e)) of entries, and the setting of entries (set(E e)) to the underlying list (Unlike Iterator, which just allows removing).