How to remove an element while traversing through elements of ArrayList using ListIterator - Java Collection Framework

Java examples for Collection Framework:ListIterator

Description

How to remove an element while traversing through elements of ArrayList using ListIterator

Demo Code

  
import java.util.ListIterator;
import java.util.ArrayList;
 
public class Main {
 
  public static void main(String[] args) {
    ArrayList aList = new ArrayList();
   //from   w  ww  .j  a v a  2  s .c  o m
    aList.add("1");
    aList.add("2");
    aList.add("3");
    aList.add("4");
    aList.add("5");
   
    ListIterator listIterator = aList.listIterator();
 
    listIterator.next();
    listIterator.next();
   
    listIterator.remove();
   
    System.out.println("After removing 2, ArrayList contains");
    for(int intIndex = 0; intIndex < aList.size(); intIndex++)
      System.out.println(aList.get(intIndex));
  }
}

Result


Related Tutorials