Friday, 5 January 2018

ListIterator in Java


This Iterator is only applicable for the List collection implemented classes like arraylist, linkedlist etc. This Iterator has bi-directional functionality. We use ListIterator to enumerate the elements of the List. ListIterator is bi-directional, whether Universal Iterator can be used only in the forward direction.


ListIterator ltr = l.listIterator();
ListIterator Object can be called using listIterator() method present in the List interface. Basically, this ListIterator extends Iterator interface with the addition of six more methods which are given below.

1. public boolean hasNext(): This method is same as in the universal Iterator. it returns True if there are more element present in the List collection object.

2. public Object next(): It is also same as the next() method in the Universal Iterator. it returns the next element.

3. public int nextIndex(): This method returns the index of the next element present in the List collection object.

4. public void hasPrevious(): It returns true if there are more elements present while traversing backwards.

5. public Object previous(): This method will be return the previous element. But if ListIterator is at the beginning then it will return -1.

6. public int previousIndex(): This method will return the index of the previous element.

7. public void remove(): It is also same as in the universal Iterator. This method is used  to remove the element from the List collection object. This method can be used only next to the previous() or next() method.

8. public void set(Object obj): This method is used to replace the last element returned by the next() or previous() method.

9. public void add(Object obj): It inserts the specified element into the list at position before the element that would be returned by the next() method.

public void set(Object obj) can throw the following exceptions:

1. IllegalArgumentException: If specified element is prevented from being added to the list.
2. IllegalStateException: If set() method is being used without the use or next() or previous() method or remove or add have been called prior calling set() method.
3. UnsupportedOperationException: If the set operation is not supported by the ListIterator.

public void add(Object obj) can throw the following exception:
1. UnsupportedOperationException: If the add operation is not supported.
2. IllegalStateException: same as the given above.
3. IllegalArgumentException: same as the given above.


import java.util.ListIterator;
import java.util.ArrayList;
import java.util.Scanner;

public class ListIteratorExample{
   public static void main(String args[]){
        Scanner in=new Scanner(;
        ArrayList<Integer> li=new ArrayList<Integer>();

        for(int i=0;i<6;i++){

        ListIterator<Integer> it= li.listIterator();


               int element=(Integer);



           System.out.print("\nAfter remove operation: \n");


              int element=(Integer);




Read More:
Enumeration Iterator in Java
Read More:  Universal Iterator in Java