ListIterator In Java

ListIterator:


ListIterator is quite a different iterator. It is only suitable for List collection implemented classes like ArrayList, LinkedList etc. It basically provides bi-directional iteration.

 

ListIterator must be used online when we want to retrieve the elements of List. This cursor has the number of the method than an iterator.

ListIterator object can be created by calling listIterator() method present in List interface.

// Here "list" is any List object, ltr is of type
// ListIterator interface and refers to "list"
ListIterator ltr = list.listIterator();

As we understand by the name ListIterator interface extends Iterator interface. So all three methods of Iterator interface are available also in ListIterator. In addition, there have six more methods.

// Forward direction

// Returns true if the iteration has more elements
public boolean hasNext();

// same as next() method of Iterator
public Object next();

// Returns the next element index 
// or list size if the list iterator
// is at the end of the list
public int nextIndex();

// Backward direction

// Returns true if the iteration has more elements
// while traversing backward
public boolean hasPrevious();

// Returns the previous element in the iteration
// and can throws NoSuchElementException
// if no more element present
public Object previous();

// Returns the previous element index 
//  or -1 if the list iterator is at the 
// beginning of the list
public int previousIndex();

// Other Methods
 
// same as remove() method of Iterator
public void remove();

// Replaces the last element returned by 
// next() or previous() with the specified element 
public void set(Object obj);

// Inserts the specified element into the list at
// position before the element that would be returned 
// by next(),
public void add(Object obj);

 

Now Clearly, we know methods that ListIterator inherits from Iterator is hasNext()next(), and remove() do exactly the same thing in both interfaces. The hasPrevious() and the previous operations are specific analogues of hasNext() and next(). The former operations refer to the element before the (implicit) cursor, whereas the latter refers to the element after the cursor. The previous operation moves the cursor backward, whereas next moves it forward.

ListIterator has no current element; its 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()

set() method can throw four exceptions

  • UnsupportedOperationException – if the operation is not supported by this list iterator than throw the UnsupportedOperationException. 
  • ClassCastException: If the class of the specified element prevents it from being added to this list. If you are added wrong data in your list.
  • IllegalArgumentException : If some features of the specified element prevent it from being added to this list.
  • IllegalStateException : If neither next nor previous have been called, or remove or add have been called after the last call to next or previous.

add() method can throw three exceptions

  • UnsupportedOperationException : If the add method is not supported by this list iterator
  • ClassCastException: If the class of the specified element prevents it from being added to this list
  • IllegalArgumentException : If some aspect of this element prevents it from being added to this list
// Java program to demonstrate ListIterator

import java.util.ArrayList;

import java.util.ListIterator;


public class Test

{

    public static void main(String[] args)

    {

        ArrayList al = new ArrayList();

        for (int i = 0; i < 10; i++)

            al.add(i);


        System.out.println(al);


        // at beginning ltr(cursor) will point to

        // index just before the first element in al

        ListIterator ltr = al.listIterator();


        // checking the next element availabilty

        while (ltr.hasNext())

        {

            //  moving cursor to next element

            int i = (Integer)ltr.next();


            // getting even elements one by one

            System.out.print(i + " ");


            // Changing even numbers to odd and

            // adding modified number again in

            // iterator

            if (i%2==0)

            {

                i++;  // Change to odd

                ltr.set(i);  // set method to change value

                ltr.add(i);  // to add

            }

        }

        System.out.println();

        System.out.println(al);

    }

}

 

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
0 1 2 3 4 5 6 7 8 9 
[1, 1, 1, 3, 3, 3, 5, 5, 5, 7, 7, 7, 9, 9, 9]

 

Limitations of ListIterator : 

It is the most powerful iterator but it is only applicable for List implemented classes, so it is not a universal iterator.

Related Articles

post a comment