Remove Element From ArrayList using Java ListIterator
Chapter:
Collections
Last Updated:
12-05-2016 20:43:28 UTC
Program:
/* ............... START ............... */
import java.util.ListIterator;
import java.util.ArrayList;
public class JavaRemoveElementListIterator {
public static void main(String[] args) {
// create an object of ArrayList
ArrayList arrayList = new ArrayList();
// Add elements to ArrayList object
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.add("4");
arrayList.add("5");
ListIterator listIterator = arrayList.listIterator();
listIterator.next();
listIterator.next();
// remove element returned by last next method
listIterator.remove();
System.out.println("After removing 2, ArrayList contains");
for (int intIndex = 0; intIndex < arrayList.size(); intIndex++)
System.out.println(arrayList.get(intIndex));
}
}
/* ............... END ............... */
Output
After removing 2, ArrayList contains
1
3
4
5
Notes:
-
Remove method can throw UnsupportedOperationException if remove operation is not supported by this ListIterator.
-
Tags
Collection, Remove Element From ArrayList using Java ListIterator, Java