Java ArrayList listIterator() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayList

Definition

In Java, listIterator() returns a ListIterator<E>, a more powerful iterator made for lists. Besides hasNext() and next(), it can move backwards with hasPrevious() and previous(), report the current position with nextIndex() and previousIndex(), and change the list with set(), add(), and remove().

listIterator(index) starts at a given position. listIterator(list.size()) starts after the last element, which is the usual way to loop backwards.

An instance method of java.util.ArrayList (Java 1.2+): a resizable-array list with index access that allows null and duplicates and is not thread-safe.

Syntax

Start at the beginning, or at any index from 0 to size().

Java
ListIterator<String> it = list.listIterator();              // before index 0
ListIterator<String> back = list.listIterator(list.size()); // after the last element
while (back.hasPrevious()) {
    System.out.println(back.previous());                     // last to first
}
CallWhat it does
list.listIterator()Starts before the first element.
list.listIterator(i)Starts so that next() returns index i.
it.set(e)Replaces the element last returned by next() or previous().
it.add(e)Inserts e at the cursor position.

A ListIterator's cursor sits between elements. next() returns the element to the right of the cursor and previous() the one to the left; nextIndex() is the index that next() would return.

Library declarations
Java
// java.util.ArrayList<E>
public ListIterator<E> listIterator()
public ListIterator<E> listIterator(int index)

// java.util.ListIterator<E> extends Iterator<E>
boolean hasPrevious()    E previous()
int nextIndex()          int previousIndex()
void set(E e)            void add(E e)

Parameters

ParameterDescription
index (int)Only in listIterator(int). The starting position, from 0 to size().

Return Value

Return ValueDescription
ListIterator<E>A new list iterator at the requested position.

Compare with Similar Methods

Other ways to loop and edit:

MethodHow it differs
iterator()Forward only, with remove() but no set or add.
set(i, e)Replaces by index in a plain for loop.
forEach(action)Read-only visit of every element.
replaceAll(op)Replaces every element with one function call.

Java Examples

Example 1 — Print a List Backwards

Start after the last element and step back with previous().

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

public class ArrayListListIteratorBack {
    public static void main(String[] args) {
        List<String> countdown = new ArrayList<>(Arrays.asList("one", "two", "three"));

        ListIterator<String> it = countdown.listIterator(countdown.size());
        while (it.hasPrevious()) {
            int i = it.previousIndex();
            System.out.println(i + " " + it.previous());
        }
        System.out.println("liftoff");
    }
}

How It Works

  1. Start at the end. listIterator(countdown.size()) puts the cursor after "three".
  2. Index first. previousIndex() reports the index that previous() is about to return, before the cursor moves.
  3. Step back. Each previous() returns the element to the left of the cursor and moves the cursor one place left.
  4. Stop at the front. Once the cursor is before index 0, there is nothing to its left and hasPrevious() becomes false.

Example 2 — Edit a List While Looping

Replace one item, insert another after a match, and remove a third, all through the iterator.

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

public class ArrayListListIteratorEdit {
    public static void main(String[] args) {
        List<String> items = new ArrayList<>(Arrays.asList("milk", "TODO", "eggs", "spam"));

        ListIterator<String> it = items.listIterator();
        while (it.hasNext()) {
            String s = it.next();
            if (s.equals("TODO")) {
                it.set("bread");
            } else if (s.equals("eggs")) {
                it.add("butter");
            } else if (s.equals("spam")) {
                it.remove();
            }
        }
        System.out.println(items);
    }
}

How It Works

  1. Replace. When next() returns "TODO", it.set("bread") overwrites that same slot.
  2. Insert after. it.add("butter") goes in right after "eggs", at the cursor, and the cursor moves past it, so "butter" is not visited by the loop.
  3. Remove. When "spam" comes up, it.remove() deletes it.
  4. No exception. All three changes went through the iterator, so it stays in sync and the loop finishes cleanly.

Example 3 — Start at an Index and Read Positions

Skip the first two marks, print index and value pairs, then try a start index that is too large.

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

public class ArrayListListIteratorIndex {
    public static void main(String[] args) {
        List<Integer> marks = new ArrayList<>(Arrays.asList(40, 55, 70, 85, 90));

        ListIterator<Integer> it = marks.listIterator(2);
        while (it.hasNext()) {
            System.out.println("index " + it.nextIndex() + " -> " + it.next());
        }

        try {
            marks.listIterator(6);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("listIterator(6) is past size 5");
        }
    }
}

How It Works

  1. Skip the first two. listIterator(2) puts the cursor before index 2, so the loop starts at 70.
  2. Position before value. nextIndex() is evaluated before next() moves the cursor, so each line shows the index of the value printed with it.
  3. To the end. The loop visits indexes 2, 3, and 4, then hasNext() is false.
  4. Allowed range. Start indexes 0 to 5 are valid for a five-element list; 6 throws IndexOutOfBoundsException.

Frequently Asked Questions

Iterator only moves forward and can remove. ListIterator also moves backward, reports indexes, and can set and add elements.
Call list.listIterator(list.size()) and loop while hasPrevious() is true, calling previous() each time.
Yes. it.add(e) inserts at the cursor without causing ConcurrentModificationException.
Any value from 0 to size(). size() means "start after the last element".
No. set replaces a value without changing the size, so the iterator stays valid.

Next: remove()

Remove one element by index or by value.

remove() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful