Java Program to Remove an Element from ArrayList using ListIterator
Last Updated :
03 Apr, 2023
ListIterator.remove() method removes the last element from the list that was returned by next() or previous() cursor positions. It can be called only once per call to next or previous. It can be made only if the operation — add(E) has not called after the last call to next or previous.
Internal working in ArrayList is shown below be its removal or addition of elements to it. Considering generic removal of elements prior to the switch to ListIterator.
Syntax:
listIterator.remove();
- Index to be removed
- Index value to be removed
Illustration:
Input: ArrayList = ["Red", "White", "Blue", "Pink"]
Output: ArrayList = ["Red", "Blue", "Pink"]
Remove element "White" or 2nd element in the ArrayList.
Input: ArrayList = [“Red”, “White”, “Blue”, “Pink”]
Output : ArrayList = [“Red”, “White”, “Blue”, “Pink”]
Remove element "Black" or 5th element in the ArrayList. Since the element that has to be removed is not in the ArrayList so nothing will be removed.
Procedure: To Remove an element from ArrayList using ListIterator is as follows:
- Create ArrayList instance new ArrayList<String>();
- Add elements in ArrayList colors using colors.add("Red");
- Create ListIterator instance of colors.listIterator();
- Print list elements before removing elements.
- Increment the iterator by listIterator.next() and move to element which you want to remove;
- Remove the element by listIterator.remove();
- Print the list after removing the element. In this example, we have removed the element "White.
State transactions while deleting an element from ArrayList

Case 1: Using loops if the index of the element to be removed is known
Java
// Java Program to Remove an element from ArrayList
// using ListIterator
// Importing ArrayList and ListIterator classes
// of java.util package
import java.util.ArrayList;
import java.util.ListIterator;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Create an ArrayList
ArrayList<String> colors = new ArrayList<String>();
// Add elements to above ArrayList
colors.add("Red");
colors.add("White");
colors.add("Blue");
colors.add("Pink");
colors.add("Black");
colors.add("Green");
// ArrayList ={Red, White, Blue, Pink, Black, Green}
ListIterator<String> listIterator
= colors.listIterator();
System.out.println("List Before remove() method = "
+ colors);
// Removing ith element from ArrayList
// using listiterator
// Suppose i = 3, so traversing
// till that element
for (int i = 0; i < 3; i++) {
listIterator.next();
}
// Removes one more element from list
// 'blue' element is removed from arraylist
listIterator.remove();
// Printing the final ArrayList after removing
// elements from originally created ArrayList
System.out.println("List After remove() method = "
+ colors);
}
}
OutputList Before remove() method = [Red, White, Blue, Pink, Black, Green]
List After remove() method = [Red, White, Pink, Black, Green]
Time Complexity: O(n)
Auxiliary Space: O(1)
As constant extra space is used.
Case 2: If the element to be removed is known
Iterator/traverse over the ArrayList and increment the list Iterator. If we reached the required element then break the loop else we reach the end and nothing will be deleted.
Java
// Java Program to Remove an element from ArrayList
// using ListIterator
// Importing ArrayList and ListIterator classes
// of java.util package
import java.util.ArrayList;
import java.util.ListIterator;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Create an ArrayList
ArrayList<String> colors = new ArrayList<String>();
// Adding elements to the arraylist
colors.add("Red");
colors.add("White");
colors.add("Blue");
colors.add("Pink");
colors.add("Black");
colors.add("Green");
ListIterator<String> listIterator
= colors.listIterator();
// Print the original ArrayList created
System.out.println("List Before remove() :- "
+ colors);
// we want to remove Blue element from the arraylist
for (String it : colors) {
listIterator.next();
// if we reached to required element break the
// loop
if (it == "Blue")
break;
}
// remove color blue from arraylist
listIterator.remove();
System.out.println("List After remove():- "
+ colors);
}
}
OutputList Before remove() :- [Red, White, Blue, Pink, black, green]
List After remove():- [Red, White, Pink, black, green]
Time Complexity: O(n)
Auxiliary Space: O(1)
As constant extra space is used.
Similar Reads
Java Program to Add an Element to ArrayList using ListIterator In this article, we will learn how to add an element to ArrayList using ListIterator. The ListIterator is used to return a list iterator over the list elements. The listIterator() returns an iterator over the list from the beginning, but the listIterator(index) returns an iterator over the list from
2 min read
Replace an Element From ArrayList using Java ListIterator To replace an element from an ArrayList, the set() method of ListIterator interface can be used. set() method of ListIterator replaces the last element which is returned by the next() or previous() methods, along with the given element. Two ways of replacing the elements using ListIterator shown bel
3 min read
Removing last element from ArrayList in Java Given an ArrayList collection in Java, the task is to remove the last element from the ArrayList. Example: Input: ArrayList[] = [10, 20, 30, 1, 2] Output: [10, 20, 30, 1] After removing the last element 2, the ArrayList is: [10, 20, 30, 1] Input: ArrayList[] = [1, 1, 2, 2, 3] Output: [1, 1, 2, 2] Af
2 min read
Java Program to Replace an Element in a List List in Java provides a way to store the ordered collection. The list Interface provides various methods to add, delete, or replace items in the List. In this article, we will learn about how we could replace an element in the list in Java. Methods to Replace an Element in a ListThere are 3 ways to
4 min read
Java Program to Remove a Specific Element From a Collection remove() method is used to remove elements from a collection. It removes the element at the specified position in this list. Shifts any subsequent elements to the left by subtracts one from their indices. In simpler words, the remove() method is used for removing the element from a specific index fr
3 min read
Java Program to Empty an ArrayList in Java ArrayList class is a resizable array, present in 'java.util package'. The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (i.e. if you want to append/add or remove element(s) to/from an array, you have to create a new array. However, elem
3 min read
Remove first element from ArrayList in Java Given an ArrayList collection in Java, the task is to remove the first element from the ArrayList. Example: Input: ArrayList[] = [10, 20, 30, 40, 50] Output: [20, 30, 40, 50]Input: ArrayList[] = [1, 1, 2, 2, 3]Output: [1, 2, 2, 3]We can use the remove() method of ArrayList container in Java to remov
2 min read
Remove all Occurrences of an Element from Array in Java In Java, removing all occurences of a given element from an array can be done using different approaches that are,Naive approach using array copyJava 8 StreamsUsing ArrayList.removeAll()Using List.removeIf()Problem Stament: Given an array and a key, the task is to remove all occurrences of the speci
5 min read
How to Replace a Element in Java ArrayList? To replace an element in Java ArrayList, set() method of java.util. An ArrayList class can be used. The set() method takes two parameters the indexes of the element that has to be replaced and the new element. The index of an ArrayList is zero-based. So, to replace the first element, 0 should be the
2 min read
Java Program to Convert an Array into a List In Java, arrays and lists are two commonly used data structures. While arrays have a fixed size and are simple to use, lists are dynamic and provide more flexibility. There are times when you may need to convert an array into a list, for instance, when you want to perform operations like adding or r
4 min read