ArrayList listIterator(int index) method in java

Let’s learn ArrayList listIterator(int index) method in java.

ArrayList listIterator(int index) method in java

listIterator(int index) method of ArrayList class returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.

The specified index indicates the first element that would be returned by an initial call to next.

An initial call to previous would return the element with the specified index minus one.

Syntax:

public ListIterator<E> listIterator(int index)

Parameters:

index index of the first element to be returned from the list iterator (by a call to next).

Throws:

IndexOutOfBoundsException – if the index is out of range(index < 0 || index > size()).

Also read – ArrayList in java

Now let’s see example on ArrayList listIterator(int index) method.

import java.util.ArrayList;
import java.util.ListIterator;
public class ArrayListListIteratorIndexMethodExample
{
   public static void main(String[] args)
   {
      ArrayList<String> al = new ArrayList<String>();
      al.add("apple");
      al.add("pineapple");
      al.add("orange");
      al.add("watermelon");
      al.add("banana");
      System.out.println("ArrayList of fruits: " + al);
      ListIterator<String> lif = al.listIterator(1);
      ListIterator<String> lib = al.listIterator(2);
      // using hasNext() and next() methods to iterate in forward direction
      System.out.println("Iterate in forward direction from second position: ");
      while(lif.hasNext())
      {
         System.out.println(lif.next());
      }
      // using hasPrevious() and previous() methods to iterate in backward direction
      System.out.println("Iterate in backward direction from second position: ");
      while(lib.hasPrevious())
      {
         System.out.println(lib.previous());
      }
   }
}


Output:

ArrayList of fruits: [apple, pineapple, orange, watermelon, banana]
Iterate in forward direction from second position:
pineapple
orange
watermelon
banana
Iterate in backward direction from second position:
pineapple
apple


Let’s see example on listIterator(int index) method IndexOutOfBoundsException.

import java.util.ArrayList;
import java.util.ListIterator;
public class ArrayListListIteratorIndexMethodExample
{
   public static void main(String[] args)
   {
      ArrayList<String> al = new ArrayList<String>();
      al.add("apple");
      al.add("pineapple");
      al.add("orange");
      al.add("watermelon");
      al.add("banana");
      System.out.println("ArrayList of fruits: " + al);
      ListIterator<String> lif = al.listIterator(1);
      ListIterator<String> lib = al.listIterator(6);
      // using hasNext() and next() methods to iterate in forward direction
      System.out.println("Iterate in forward direction from second position: ");
      while(lif.hasNext())
      {
         System.out.println(lif.next());
      }
      // using hasPrevious() and previous() methods to iterate in backward direction
      System.out.println("Iterate in backward direction from second position: ");
      while(lib.hasPrevious())
      {
         System.out.println(lib.previous());
      }
   }
}


Output:

ArrayList of fruits: [apple, pineapple, orange, watermelon, banana]
Exception in thread “main” java.lang.IndexOutOfBoundsException: Index: 6, Size: 5