Java ArrayList iterator() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayList

Definition

In Java, iterator() returns an Iterator<E> that visits the elements of an ArrayList in index order, from 0 to the end. You call hasNext() to ask whether more elements remain and next() to get the next one.

The enhanced for loop (for (String s : list)) uses an iterator behind the scenes. Calling iterator() yourself is useful mainly when you need it.remove(), the safe way to delete the current element while looping.

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

Get an iterator, then loop with hasNext() and next().

Java
Iterator<String> it = list.iterator();
while (it.hasNext()) {
    String s = it.next();
    if (s.isEmpty()) {
        it.remove();          // safe removal
    }
}
CallWhat it does
list.iterator()A new iterator positioned at the start.
it.hasNext()true if another element remains.
it.next()Returns the next element and moves forward.
it.remove()Removes the element last returned by next().

The iterator is fail-fast: if the list is structurally changed (elements added or removed) by anything other than this iterator, its next next() call usually throws ConcurrentModificationException. Replacing values with set() is not a structural change.

Library declarations
Java
// java.util.ArrayList<E>
public Iterator<E> iterator()

// java.util.Iterator<E>
boolean hasNext()
E next()
void remove()

Parameters

ParameterDescription
Noneiterator() takes no parameters.

Return Value

Return ValueDescription
Iterator<E>A new iterator positioned before the first element.

Compare with Similar Methods

Other ways to walk an ArrayList:

MethodHow it differs
listIterator()Goes both ways and can also set and add.
forEach(action)Runs a lambda for each element; no removal.
removeIf(filter)Removes by condition without writing the loop yourself.
spliterator()Splittable traversal used by streams.

Java Examples

Example 1 — Walk a List with hasNext() and next()

Number each color as the iterator returns it.

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

public class ArrayListIteratorDemo {
    public static void main(String[] args) {
        List<String> colors = new ArrayList<>(Arrays.asList("red", "green", "blue"));

        Iterator<String> it = colors.iterator();
        int n = 0;
        while (it.hasNext()) {
            n++;
            System.out.println(n + ": " + it.next());
        }
        System.out.println("more? " + it.hasNext());
    }
}

How It Works

  1. Get an iterator. colors.iterator() starts just before the first element.
  2. Loop test. hasNext() is true while elements remain; each next() returns one and moves past it.
  3. Index order. The colors come out exactly as stored: red, green, blue.
  4. Used up. After the loop, hasNext() is false. An iterator cannot be rewound; call iterator() again for a second pass.

Example 2 — Remove Expired Items with it.remove()

Remove every 0 (an expired item) while walking the list.

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

public class ArrayListIteratorRemove {
    public static void main(String[] args) {
        List<Integer> days = new ArrayList<>(Arrays.asList(5, 0, 12, 0, 3));

        Iterator<Integer> it = days.iterator();
        int removed = 0;
        while (it.hasNext()) {
            if (it.next() == 0) {
                it.remove();
                removed++;
            }
        }
        System.out.println("removed " + removed + ", left " + days);
    }
}

How It Works

  1. Days left. Each number is how many days an item has before it expires; 0 means it has expired.
  2. Unboxed comparison. it.next() == 0 compares an Integer with an int, so Java unboxes it and compares the numbers.
  3. Remove through the iterator. it.remove() deletes the element that next() just returned and keeps the iterator in step with the list.
  4. No skipping. Both zeros are removed, even though they are not next to each other.

Example 3 — What Fails: Changing the List Behind the Iterator

Add to the list directly while an iterator is in use, then call remove() too early.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.List;

public class ArrayListIteratorFailFast {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>(Arrays.asList("Al", "Bo", "Cy"));
        Iterator<String> it = names.iterator();

        System.out.println("first: " + it.next());
        names.add("Di");

        try {
            it.next();
        } catch (ConcurrentModificationException e) {
            System.out.println("next() after add: " + e.getClass().getSimpleName());
        }

        try {
            names.iterator().remove();
        } catch (IllegalStateException e) {
            System.out.println("remove() before next(): " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. One step taken. The iterator returns "Al" and remembers how many structural changes the list had when it was created.
  2. Change behind its back. names.add("Di") modifies the list directly, not through the iterator.
  3. Fail-fast. The next call to it.next() detects the change and throws ConcurrentModificationException.
  4. Wrong order. A fresh iterator has not returned anything yet, so calling remove() on it throws IllegalStateException.

Frequently Asked Questions

Use an explicit Iterator and call it.remove(), or use list.removeIf(condition). Do not call list.remove() inside a for-each loop.
The list was structurally changed (an add or remove) while an iterator, often the hidden one in a for-each loop, was walking it.
Yes. for (E e : list) is compiled into iterator(), hasNext(), and next() calls. It just gives you no access to it.remove().
No. Use listIterator(), which adds hasPrevious() and previous().
No. Call list.iterator() again to get a fresh one that starts at the beginning.

Next: lastIndexOf()

Find the last index of a value, or -1.

lastIndexOf() 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.

12 people found this page helpful