.listIterator()
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 givenArrayList
collection. T
: The type of elements in theArrayList
.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 ArrayListArrayList<String> arrayList = new ArrayList<>();arrayList.add("Nice");arrayList.add("to");arrayList.add("meet");arrayList.add("you");// Creating a ListIterator from ArrayListListIterator<String> it = arrayList.listIterator();// Iterating through the elements and printing themwhile (it.hasNext()) {System.out.println(it.next());}}}
The output of the above example code is as follows:
Nicetomeetyou
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 ArrayListArrayList<String> arrayList = new ArrayList<>();arrayList.add("Nice");arrayList.add("to");arrayList.add("meet");arrayList.add("you");// Creating a ListIterator from ArrayList at index 2ListIterator<String> it = arrayList.listIterator(2);// Iterating through the remaining elements and printing themwhile (it.hasNext()) {System.out.println(it.next());}}}
The output of the above example code is as follows:
meetyou
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 ArrayListArrayList<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 5ListIterator<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: 4Exception: java.lang.IndexOutOfBoundsException: Index: 5, Size: 4
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Java
Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.Beginner Friendly17 hours