Java ArrayListlistIterator() Method
Example
Use aListIterator to loop forward and backward through a list:
import java.util.ArrayList;import java.util.ListIterator;public class Main { public static void main(String[] args) { // Make a collection ArrayList<String> cars = new ArrayList<String>(); cars.add("Volvo"); cars.add("BMW"); cars.add("Ford"); cars.add("Mazda"); // Get the iterator ListIterator<String> it = cars.listIterator(); // Loop through the list while(it.hasNext()) { System.out.println(it.next()); } System.out.println("---"); // Loop backwards through the list while(it.hasPrevious()) { System.out.println(it.previous()); } }}Definition and Usage
ThelistIterator() method returns aListIterator for the list.
To learn how to use iterators, see ourJava Iterator tutorial.
TheListIterator differs from anIterator in that it can also traverse the list backwards.
Syntax
public ListIterator listIterator()Technical Details
| Returns: | AListIterator object. |
|---|
Related Pages
❮ ArrayList Methods

