ListIterator walks a list in both directions and can set or add elements at the cursor, which a plain Iterator cannot.
var list = new ArrayList<>(List.of("a", "b", "c"));
ListIterator<String> it = list.listIterator();
while (it.hasNext()) {
int i = it.nextIndex();
String v = it.next();
if (v.equals("b")) it.set("B");
System.out.println("forward " + i + " -> " + v);
}
while (it.hasPrevious()) {
System.out.println("backward " + it.previousIndex() + " -> " + it.previous());
}
System.out.println("final list = " + list);
forward 0 -> a
forward 1 -> b
forward 2 -> c
backward 2 -> c
backward 1 -> B
backward 0 -> a
final list = [a, B, c]
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-07-30