.listIterator()

Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Jul 11, 2024
Contribute to Docs

The ArrayList class in Java provides the .listIterator() method to obtain a ListIterator for its elements. The default method returns a ListIterator that points to the first element in the ArrayList. This iterator allows for traversing the list in both forward and backward directions and supports modifications to the list during iteration.

Syntax

public ListIterator<T> it = list.listiterator();
public ListIterator<T> it = list.listiterator(int index);
  • This creates a ListIterator on the given ArrayList collection.
  • T: The type of elements in the ArrayList.
  • index: The starting position of the iterator in the list. It must be a valid index in the list (0 ≤ index ≤ size of the list). This is an optional parameter.

Example 1

In this example, the .listiterator() method is called on an ArrayList instance named arrayList to obtain a ListIterator for its elements:

import java.util.ArrayList;
import java.util.ListIterator;
public class ArrayListListIteratorExample {
public static void main(String[] args) {
// Creating an ArrayList
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("Nice");
arrayList.add("to");
arrayList.add("meet");
arrayList.add("you");
// Creating a ListIterator from ArrayList
ListIterator<String> it = arrayList.listIterator();
// Iterating through the elements and printing them
while (it.hasNext()) {
System.out.println(it.next());
}
}
}

The output of the above example code is as follows:

Nice
to
meet
you

Example 2

The following example demonstrates the creation of a ListIterator at a specified index in an ArrayList:

import java.util.ArrayList;
import java.util.ListIterator;
public class ArrayListListIteratorExample {
public static void main(String[] args) {
// Creating an ArrayList
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("Nice");
arrayList.add("to");
arrayList.add("meet");
arrayList.add("you");
// Creating a ListIterator from ArrayList at index 2
ListIterator<String> it = arrayList.listIterator(2);
// Iterating through the remaining elements and printing them
while (it.hasNext()) {
System.out.println(it.next());
}
}
}

The output of the above example code is as follows:

meet
you

Example 3

This example showcases exception handling when attempting to create an iterator at an invalid index:

import java.util.ArrayList;
import java.util.ListIterator;
public class ArrayListListIteratorExample {
public static void main(String[] args) {
// Creating an ArrayList
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("Nice");
arrayList.add("to");
arrayList.add("meet");
arrayList.add("you");
System.out.println("ArrayList size: " + arrayList.size());
try {
// Attempting to create a ListIterator at index 5
ListIterator<String> it = arrayList.listIterator(5);
} catch (IndexOutOfBoundsException e){
System.out.println("Exception: " + e);
}
}
}

The output of the above example code is as follows:

ArrayList size: 4
Exception: java.lang.IndexOutOfBoundsException: Index: 5, Size: 4

All contributors

Looking to contribute?

Learn Java on Codecademy