Menu
Topics Index
...
`


Collections Framework > Iterator >
Siva Nookala - 14 Apr 2016
ListIterator is used to traverse the list in either direction, modify the list during iteration, and obtain the iterator's current position in the list.

ListIterator is available to only those collections that implement the Java List Interface . A listIterator has no current element, it's cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next().

Advantages of using listIterator over iterator:
  • With iterator you can move only forward, but with listIterator you can also move reverse while reading the elements.
  • With listIterator you can obtain the index at any point while traversing, which is not possible with iterator.
  • With iterator you can only check whether the next element is available or not, but with listIterator you can check the previous and next elements.
  • With listIterator you can add or modify an element at any point while traversing, which is not possible with iterator.

ListIterator Methods:
MethodDescription
void add(Object obj)Inserts the specified element into the list.
boolean hasNext( )Returns true if this list iterator has more elements when traversing the list in the forward direction.
boolean hasPrevious( )Returns true if this list iterator has more elements when traversing the list in the reverse direction.
Object next( )Returns the next element in the list.
int nextIndex( )Returns the index of the element that would be returned by a subsequent call to next.
Object previous( )Returns the previous element in the list.
int previousIndex( )Returns the index of the element that would be returned by a subsequent call to previous.
void remove( )Removes from the list the last element that was returned by next or previous.
void set(Object obj)Replaces the last element returned by next or previous with the specified element.

ListIterator Demo
import java.util.*;

class ListIteratorDemo
{
    public static void main(String arg[])
    {
        ArrayList ar = new ArrayList();
        ar.add("Black");
        ar.add("Red");
        ar.add("Blue");
        ListIterator litr = ar.listIterator();
        while (litr.hasNext()) // In forward direction
        {
            System.out.print(litr.next() + " ");
        }
        System.out.println();
        while (litr.hasPrevious()) // In reverse direction
        {
            System.out.print(litr.previous() + " ");
        }
        System.out.println();
        litr = ar.listIterator(2); // LINE A - Set iterator at specified index
        System.out.println(litr.previousIndex() + " " + litr.nextIndex()); // Indices
        litr.add("Orange"); // LINE B
        System.out.println("After adding Orange : " + litr.previous());
        litr.remove(); // LINE C
        System.out.println("After removing : " + litr.previous());
        litr.set("Yellow"); // LINE D
        System.out.println("After setting : " + litr.next());    
    }
}
OUTPUT

Black Red Blue
Blue Red Black
1 2
After adding Orange : Orange
After removing : Red
After setting : Yellow

DESCRIPTION

In this example, the elements in the list are printed by traversing in either direction. Iterator is set at index 2 and the indices are printed. An element "Orange" is added in LINE A and printed, then the element "Orange" is removed in LINE B and the previous element "Red" is printed and finally the element "Red" is replaced with "Yellow" in LINE C and printed.

THINGS TO TRY
  • Add elements "Green", "Purple", "Indigo" to the list using add method as shown in LINE B.
  • Try for the below code.
    ArrayList ar = new ArrayList();
    ar.add("Black");
    ar.add("Red");
    ar.add("Blue");
    ListIterator litr = ar.listIterator();
    System.out.print(litr.next() + " ");
    System.out.print(litr.next() + " ");
    System.out.print(litr.next() + " ");
    The output for the above code is :
    Black Red Blue when litr.next is invoked for the first time iterator begins at the first element and when it is invoked again it moves to the next element.
  • Remove the element Blue from the list using remove method.

Dependent Topics : Java List Interface  

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.