Java Collection Tutorial - Java ListIterator.remove()








Syntax

ListIterator.remove() has the following syntax.

void remove()

Example

In the following code shows how to use ListIterator.remove() method.

/*ww w  . j  a va2 s.  c o  m*/
import java.util.ArrayList;
import java.util.ListIterator;

public class Main {
  public static void main(String[] args) {
    ArrayList<String> aList = new ArrayList<String>();

    aList.add("1");
    aList.add("2");
    aList.add("3");
    aList.add("4");
    aList.add("5");

    // Get an object of ListIterator using listIterator() method

    ListIterator listIterator = aList.listIterator();
    listIterator.next();
    listIterator.next();

    // remove element returned by last next method

    listIterator.remove();
    for (String str: aList){
      System.out.println(str);
    }
  }
}

The code above generates the following result.