Java ArrayList.listIterator()

Syntax

ArrayList.listIterator() has the following syntax.

public ListIterator <E> listIterator()

Example

In the following code shows how to use ArrayList.listIterator() method.

It shows how we can use ListIterator to remove elements from an ArrayList.


/* w w  w .ja va2s  .co m*/

import java.util.ArrayList;
import java.util.ListIterator;

public class Main {
  public static void main(String args[]) {
    ArrayList<Integer>  arrlist = new ArrayList<Integer> ();
  
    arrlist.add(1);
    arrlist.add(2);
    arrlist.add(3);
    arrlist.add(4);
    arrlist.add(5);

    
    ListIterator<Integer> iterator = arrlist.listIterator();
    while(iterator.hasNext()){
      Integer i = iterator.next();
      iterator.remove();
      
    }
    System.out.println(arrlist);

  }
}

The code above generates the following result.