

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove an element from an ArrayList using the ListIterator in Java
An element can be removed from an ArrayList using the ListIterator method remove(). This method removes the current element in the ArrayList. If the remove() method is not preceded by the next() method, then the exception IllegalStateException is thrown.
A program that demonstrates this is given as follows.
Example
import java.util.ArrayList; import java.util.ListIterator; public class Demo { public static void main(String[] args) { ArrayList<String> aList = new ArrayList<String>(); aList.add("Apple"); aList.add("Mango"); aList.add("Guava"); aList.add("Orange"); aList.add("Peach"); System.out.println("The ArrayList elements are: "); for (String s: aList) { System.out.println(s); } ListIterator li = aList.listIterator(); li.next(); li.remove(); System.out.println("\nThe element Apple is removed"); System.out.println("\nThe ArrayList elements are: "); for (String s: aList) { System.out.println(s); } } }
Output
The output of the above program is as follows −
The ArrayList elements are: Apple Mango Guava Orange Peach The element Apple is removed The ArrayList elements are: Mango Guava Orange Peach
- Related Questions & Answers
- Replace an element in an ArrayList using the ListIterator in Java
- Java Program to remove an element from List with ListIterator
- How to remove an element from ArrayList in Java?
- Replace an element from a Java List using ListIterator
- Java Program to Remove Repeated Element from An ArrayList
- Iterate through an ArrayList using a ListIterator in Java
- How to remove an element from ArrayList or, LinkedList in Java?
- Retrieve an element from ArrayList in Java
- Insert an element to List using ListIterator in Java
- Remove duplicate items from an ArrayList in Java
- How to remove duplicates from an ArrayList in Java?
- Remove an element from IdentityHashMap in Java
- Obtain the Previous Index and Next Index in an ArrayList using the ListIterator in Java
- Use ListIterator to traverse an ArrayList in the forward direction in Java
- Use ListIterator to traverse an ArrayList in the reverse direction in Java
Advertisements