What is the difference between Iterator and ListIterator in Java?
A ListIterator is a bigger version of an Iterator. It can do everything that the plain one can do, and it can do four more things on top.
The plain Iterator gives you three methods. The method hasNext tells you whether there is another element. The method next gives you the next element and moves on. The method remove removes the element that next last gave you.
The ListIterator adds four more. The methods hasPrevious and previous let you walk backwards through the list. The methods nextIndex and previousIndex tell you the position of the element that you are about to get.
And it can also change the list as it goes. The method add puts a new element in before the one that comes next, and the method set replaces the element that the iterator last gave you.
One more difference is about where you can get one. Every collection that implements the Collection interface can give you a plain iterator. Only the classes that implement the List interface can give you a ListIterator, because walking backwards only makes sense for something that has an order.
The next question in this topic looks at how to use that.