The listIterator() method of the ArrayList class returns a ListIterator that allows traversing the elements of an ArrayList in both forward and backward directions. Unlike a normal Iterator, a ListIterator also supports adding, removing, and modifying elements during iteration.
- Supports both forward and backward traversal.
- Allows adding, updating, and removing elements while iterating.
import java.util.ArrayList;
import java.util.ListIterator;
public class GFG {
public static void main(String[] args) {
// Create an ArrayList
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Mango");
// Obtain a ListIterator
ListIterator<String> itr = fruits.listIterator();
System.out.println("Forward Traversal:");
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
}
Output
Forward Traversal: Apple Banana Mango
Explanation: In the above example, listIterator() returns a ListIterator positioned at the beginning of the ArrayList. The hasNext() and next() methods are used to traverse all elements in forward order.
Syntax
public ListIterator<E> listIterator()
public ListIterator<E> listIterator(int index)
- Parameters: index: Specifies the starting position of the iterator.
- Return Value: Returns a
ListIteratorfor traversing the elements of theArrayList. - Exception: IndexOutOfBoundsException: Thrown if the specified index is less than
0or greater than the size of the list.
Example: Starting Iteration from a Specific Index
import java.util.ArrayList;
import java.util.ListIterator;
public class GFG {
public static void main(String[] args) {
// Create an ArrayList
ArrayList<String> colors = new ArrayList<>();
colors.add("Red");
colors.add("Green");
colors.add("Blue");
colors.add("Yellow");
// Start iteration from index 2
ListIterator<String> itr = colors.listIterator(2);
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
}
Output
Blue Yellow
Explanation: The iterator starts from index 2, so only the elements from index 2 onward are traversed.
Example: Traversing in Both Directions
import java.util.ArrayList;
import java.util.ListIterator;
public class GFG {
public static void main(String[] args) {
// Create an ArrayList
ArrayList<String> cities = new ArrayList<>();
cities.add("Delhi");
cities.add("Mumbai");
cities.add("Chennai");
ListIterator<String> itr = cities.listIterator();
// Forward traversal
while (itr.hasNext()) {
System.out.println(itr.next());
}
System.out.println("Backward Traversal:");
// Backward traversal
while (itr.hasPrevious()) {
System.out.println(itr.previous());
}
}
}
Output
Delhi Mumbai Chennai Backward Traversal: Chennai Mumbai Delhi
Explanation: Unlike a normal Iterator, a ListIterator can move in both directions using next() and previous().
Difference Between Iterator and ListIterator
| Iterator | ListIterator |
|---|---|
| Traverses only in the forward direction. | Traverses in both forward and backward directions. |
| Supports next() and remove(). | Supports next(), previous(), add(), set(), and remove(). |
| Works with all Collection types. | Works only with List implementations. |
| Cannot obtain the previous element. | Can access both previous and next elements. |
Advantages of listIterator() Method
- Supports both forward and backward traversal.
- Can start iteration from any valid index.
- Allows adding, updating, and removing elements during iteration.
- Useful for bidirectional navigation of list elements.
- Provides more functionality than a standard Iterator.