Java ArrayList ListIterator example shows how to iterate ArrayList using ListIterator. The example also shows how to iterate ArrayList in backward direction using ListIterator.
How to iterate ArrayList using ListIterator?
ArrayList class provides listIterator
method which returns a list iterator over its elements.
1 |
public ListIterator<E> listIterator() |
Once we get the list iterator, we can iterate the ArrayList using hasNext
, next
, hasPrevious
and previous
methods.
1 |
boolean hasNext() |
This method returns true if there are more elements to iterate when iterating the ArrayList in the forward direction.
1 |
E next() |
This method returns the next element in the ArrayList and increments the cursor.
1 |
boolean hasPrevious() |
This method returns true if there more elements to iterate when iterating the ArrayList in a backward direction.
1 |
E previous() |
This method returns the previous element in the ArrayList and decrements the cursor.
Java ArrayList ListIterator Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; import java.util.ListIterator; public class ArrayListListIteratorExample { public static void main(String[] args) { //Create an ArrayList ArrayList<String> aListNumbers = new ArrayList<String>(); //Add elements aListNumbers.add("One"); aListNumbers.add("Two"); aListNumbers.add("Three"); aListNumbers.add("Four"); aListNumbers.add("Five"); /* * To get ListIterator, use * public ListIterator<E> listIterator() * method of ArrayList class. */ ListIterator<String> listItr = aListNumbers.listIterator(); /* * Iterating ArrayList in forward direction: * * Use hasNext() method of list iterator * to check if there are any more elements * and next() method to get the next element * from the ArrayList. */ System.out.println("Iterating ArrayList in forward direction"); while( listItr.hasNext() ) System.out.println( listItr.next() ); /* * Iterating ArrayList in backward direction: * * Use hasPrevious() method of list iterator * to check if there are any more elements * and previous() method to get the previous element * from the ArrayList. */ System.out.println("Iterating ArrayList in backward direction"); while( listItr.hasPrevious() ) System.out.println( listItr.previous() ); } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 |
Iterating ArrayList in forward direction One Two Three Four Five Iterating ArrayList in backward direction Five Four Three Two One |
In the above example, we first iterated the ArrayList in the forward direction which put the cursor at the end of the list. We immediately iterated the ArrayList in the backward direction so it started from the end of the ArrayList.
If you want to iterate the list from backward direction without first iterating it in the forward direction then use overloaded listIterator
method which accepts the starting position.
1 |
ListIterator<E> listIterator(int startIndex) |
This method returns a list iterator over elements of an ArrayList starting at the specified position.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
//Create an ArrayList ArrayList<String> aListNumbers = new ArrayList<String>(); //Add elements aListNumbers.add("One"); aListNumbers.add("Two"); aListNumbers.add("Three"); aListNumbers.add("Four"); aListNumbers.add("Five"); /* * To iterate list in backward direction, use listIterator * method with starting index at end of the list. */ ListIterator<String> listItr = aListNumbers.listIterator( aListNumbers.size() ); while( listItr.hasPrevious() ) System.out.println( listItr.previous() ); |
Output
1 2 3 4 5 |
Five Four Three Two One |
How to remove elements using list iterator?
If you want to remove elements while iterating the ArrayList, use
1 |
void remove() |
This method removes the last element that was returned by next
or previous
method call from the ArrayList.
1 2 3 4 5 6 7 8 9 |
ListIterator<String> listItr = aListNumbers.listIterator(); while( listItr.hasNext()){ if( listItr.next().equals("Two") ) listItr.remove(); } System.out.println(aListNumbers); |
Output
1 |
[One, Three, Four, Five] |
How to replace elements using list iterator?
If you want to replace elements while iterating the ArrayList, use
1 |
void set(E e) |
This method replaces the last element that was returned by next
or previous
method call with the specified element in the ArrayList.
1 2 3 4 5 6 7 8 9 10 |
ListIterator<String> listItr = aListNumbers.listIterator(); while( listItr.hasNext()){ //element "Five" will be replaced with "Ten" if( listItr.next().equals("Five") ) listItr.set("Ten"); } System.out.println(aListNumbers); |
Output
1 |
[One, Two, Three, Four, Ten] |
How to insert or add elements using list iterator?
If you want to insert or add elements while iterating the ArrayList using list iterator, use
1 |
void add(E e) |
This method inserts an element to the ArrayList at the current cursor position.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
ListIterator<String> listItr = aListNumbers.listIterator(); //this will insert an element at the start of the ArrayList listItr.add("Zero"); while( listItr.hasNext()){ //this will insert "Thirty" after element "Three" if( listItr.next().equals("Three") ) listItr.add("Thirty"); } System.out.println(aListNumbers); |
Output
1 |
[Zero, One, Two, Three, Thirty, Four, Five] |
This example is a part of the Java ArrayList tutorial with examples and Java ListIterator tutorial with examples.
Please let me know your views in the comments section below.