Home Java ArrayList Methods iterator() Java ArrayList iterator() Method 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().
Iterator<String> it = list.iterator();
while (it.hasNext()) {
String s = it.next();
if (s.isEmpty()) {
it.remove(); // safe removal
}
} Call What 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.util.ArrayList<E>
public Iterator<E> iterator()
// java.util.Iterator<E>
boolean hasNext()
E next()
void remove() Parameters Parameter Description None iterator() takes no parameters.
Return Value Return Value Description Iterator<E>A new iterator positioned before the first element.
Important
next() throws NoSuchElementException when no elements remain. it.remove() throws IllegalStateException if next() has not been called yet, or if remove() was already called for the current element. Fail-fast checking is best-effort, so never rely on ConcurrentModificationException for program logic.
Compare with Similar Methods Other ways to walk an ArrayList:
Java Examples 1. Walk the list 2. Remove safely 3. What fails Example 1 — Walk a List with hasNext() and next() Number each color as the iterator returns it.
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());
}
} 1: red
2: green
3: blue
more? false How It Works Get an iterator. colors.iterator() starts just before the first element.Loop test. hasNext() is true while elements remain; each next() returns one and moves past it.Index order. The colors come out exactly as stored: red, green, blue.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.
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);
}
} removed 2, left [5, 12, 3] How It Works Days left. Each number is how many days an item has before it expires; 0 means it has expired.Unboxed comparison. it.next() == 0 compares an Integer with an int, so Java unboxes it and compares the numbers.Remove through the iterator. it.remove() deletes the element that next() just returned and keeps the iterator in step with the list.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.
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());
}
}
} first: Al
next() after add: ConcurrentModificationException
remove() before next(): IllegalStateException How It Works One step taken. The iterator returns "Al" and remembers how many structural changes the list had when it was created.Change behind its back. names.add("Di") modifies the list directly, not through the iterator.Fail-fast. The next call to it.next() detects the change and throws ConcurrentModificationException.Wrong order. A fresh iterator has not returned anything yet, so calling remove() on it throws IllegalStateException.Frequently Asked Questions How do I remove elements while iterating over an ArrayList? Use an explicit Iterator and call it.remove(), or use list.removeIf(condition). Do not call list.remove() inside a for-each loop.
Why do I get ConcurrentModificationException? The list was structurally changed (an add or remove) while an iterator, often the hidden one in a for-each loop, was walking it.
Is the enhanced for loop the same as using iterator()? Yes. for (E e : list) is compiled into iterator(), hasNext(), and next() calls. It just gives you no access to it.remove().
Can an Iterator go backwards? No. Use listIterator(), which adds hasPrevious() and previous().
Can I reuse an iterator after it reaches the end? No. Call list.iterator() again to get a fresh one that starts at the beginning.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion