A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/arraylist-listiterator-method-in-java-with-examples/ below:

ArrayList listIterator() Method in Java

ArrayList listIterator() Method in Java

Last Updated : 06 Aug, 2025

The listIterator() method of ArrayList class in Java is used to return a ListIterator for traversing the elements of the list in proper sequence. The iterator allows both forward and backward traversal of the list.

Example 1: Here, we use the listIterator() method to obtain a ListIterator over all elements in an ArrayList.

Java
import java.util.ArrayList;
import java.util.ListIterator;

public class GFG {
    public static void main(String[] args) {
      
        // Create an ArrayList of Strings
        ArrayList<String> al = new ArrayList<>();
        al.add("A");
        al.add("B");
        al.add("C");
        al.add("D");

        // Obtain a ListIterator
        ListIterator<String> i = al.listIterator();

        System.out.println("Iterating through the list:");
        while (i.hasNext()) {
            System.out.println(i.next());
        }
    }
}

Output
Iterating through the list:
A
B
C
D

Explanation: In the above example, we use a ListIterator to iterate over an ArrayList of Strings i.e. "A", "B", "C", and "D". It prints each element of the list sequentially using the hasNext() and next() methods of the ListIterator.

Syntax of ArrayList listIterator() Method

// Obtain a ListIterator over all elements
public ListIterator<E> listIterator()

// Obtain a ListIterator starting at a specified index
public ListIterator<E> listIterator(int index)

There are two versions of listIterator() method i.e. one to obtain a ListIterator over all elements and one to obtain a ListIterator starting at a specified index.

Example 2: Here, we use the listIterator() method to obtain a ListIterator starting at a specified index.

Java
import java.util.ArrayList;
import java.util.ListIterator;

public class GFG {
    public static void main(String[] args) {
      
        // Create an ArrayList of Strings
        ArrayList<String> al = new ArrayList<>();
        al.add("A");
        al.add("B");
        al.add("C");
        al.add("D");

        // Obtain a ListIterator 
        // starting at index 2
        ListIterator<String> i = al.listIterator(2);

        System.out.println("Iterating from index 2:");
        while (i.hasNext()) {
            System.out.println(i.next());
        }
    }
}

Output
Iterating from index 2:
C
D

Example 3: Here, we will see how the below code handles an invalid index with listIterator() method. 

Java
// listIterator() method
// for IndexOutOfBoundsException
import java.util.*;

public class GFG {
    public static void main(String[] argv) throws Exception {
        try {

            ArrayList<String>
                al = new ArrayList<String>();

            // adding element to arrlist
            al.add("A");
            al.add("B");
            al.add("C");
            al.add("D");

            // print arrlist
            System.out.println("ArrayList: "
                               + al);
            System.out.println("Size of ArrayList: "
                               + al.size());

            // Get ListIterator from index 7
            // using listIterator() method
            System.out.println("Trying to getListIterator"
                               + " from index 7\n");

            ListIterator<String>
                i = al.listIterator(7);
        }

        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception thrown: " + e);
        }
    }
}

Output
ArrayList: [A, B, C, D]
Size of ArrayList: 4
Trying to getListIterator from index 7

Exception thrown: java.lang.IndexOutOfBoundsException: Index: 7, Size: 4


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4