VOOZH about

URL: https://www.geeksforgeeks.org/java/arraylist-listiterator-method-in-java-with-examples/

⇱ ArrayList listIterator() Method in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

ArrayList listIterator() Method in Java

Last Updated : 6 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.


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.

  • Parameters: For specified index, method takes the index of the first element as a parameter to be returned from the list iterator (by a call to next).
  • Return Value: This method returns a ListIterator object that can be used to iterate over the elements of the list.

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


Output
Iterating from index 2:
C
D


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


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
Comment