ListIterator
java.util.List,the List interface places additional methods, other than those specified in the Collection interface.
On the contracts of the iterator, add, remove.. Some additional methods are added in the List interface. Two of them are here
public abstract java.util.ListIterator listIterator();
public abstract java.util.ListIterator listIterator(int);
The List interface provides a special iterator, called a ListIterator,That allows element insertion and replacement, and bidirectional access, means you can travel through iterator forward and backward while iterating you can add and remove objects from the List implementations.
You can not modify the List directly while iterates through the Iterators
See the following example of ListIterator
| import java.util.Arrays;
import java.util.ListIterator; import java.util.List; public class ListIteratorTest { public static void main(String args[]) { List nameList = new ArraysList(); ListIterator itr = nameList.listIterator(); while (itr.hasNext()) { String name = itr.next(); System.out.println(name); // based on some operation you are adding object to list if (name.equals(“three”)) { itr.add(“four”); } } } |