What is the diff between Iterator and ListIterator ?

-Iterator enables you to cycle through the collection. It is unidirectional.
ListIterator extends iterator. It allows bidirectional traversal of a list.
-While iterating the elements by Iterator we can perform only read and remove operations. But by using ListIterator we can perform read,removal, replace and addition of new objects also.
ListIterator is available only to those collection which implement List interface.
-Iterator can be get by using iterator() of Collection interface where as ListIterator can be get by using listIterator() method of List interface.
import java.util.*;
class demo1
{
public static void main(String[] args){
ArrayList al=new ArrayList();
al.add("bhabani");
al.add("samar");
al.add("gaura");
al.add("milan");
al.add("chandan");
ListIterator li=al.listIterator();
while(li.hasNext()){System.out.println(li.next());}
System.out.println("\n\nreverse order\n");
while(li.hasPrevious()){System.out.println(li.previous());}
System.out.println(al);
}};

No comments:

Post a Comment