The ArrayList in Java is considered a resizable array that adjusts its size according to the elements added or removed. It is implemented using the ArrayList class which belongs to the “java.util” package. Multiple methods are present in the ArrayList class. Among these methods the add() and remove() methods are considered the simplest and the most frequently used.
In this write-up, we will remove elements from the ArrayList in Java using different methods.
How to Remove/Delete an Element From ArrayList in Java?
The element from the ArrayList in Java can be removed by either declaring the index or by declaring the element itself. In Java, certain methods are used that tend to effectively delete the desired elements from the ArrayList. These methods are listed below:
- remove() Method
- removeIf() Method
- listIterator.remove() Method
- Using valueOf() with remove() Method
Method 1: remove() Method to Remove the Element
The remove() method in Java is the most simple and easy method to delete the element from the ArrayList in Java. This method removes the element using two different ways that are:
- Removing the element by Index.
- Removing by declaring the element itself.
Example 1: Remove the Element by Index Using the remove() Method
This method is implemented by declaring the index for the element to be removed in the ArrayList. The below syntax is in Java and the code implements the remove() method.
remove(int Index)
The index must be declared in the Integer format. The declared syntax is implemented in the code below.
import java.util.ArrayList;
//Declaring the class
public class RemoveExample1 {
public static void main(String[] args) {
//Declaring an ArrayList
ArrayList<String> array1 = new ArrayList<String>();
// Use the add() method
array1.add("Python");
array1.add("C++");
array1.add("Javascript");
array1.add("Ruby");
array1.add("Java");
//Use the size() method of Java
System.out.println("The Size of the ArrayList is: " + array1.size());
//Print all the elements of the ArrayList
for (String pl: array1) {
System.out.println("Programming Language is: " + pl);
}
// Remove the element at Index 3
array1.remove(3);
System.out.println("\nThe Size of the ArrayList after removing the element is : " + array1.size());
// Print all the elements present in the ArrayList
for (String pl: array1) {
System.out.println("Programming Language is: " + pl);
}
}
}
In the above code block:
- The “java.util.ArrayList” package is imported for declaring the ArrayList.
- In the main() method of Java, an object is created as “array1” for declaring the ArrayList.
- The next step in the code involves the implementation of the add() method to insert the elements.
- The “for loop” iterates over the elements in the ArrayList and these elements are printed using the println() method.
- The further step involves deleting the element at index “3” using the remove() method of Java.
- The size of the original ArrayList and the modified ArrayList are printed using the println() method.
Output
In the below output, the size of the ArrayList before the removal is “5” and after the deletion of the element is “4” since the element “Ruby” at index “3” is removed.
Example 2: Remove by Declaring the Value in the ArrayList
The next approach to implement the remove() method is to pass the value itself that is to be removed to the remove() method. The syntax along with its code is implemented below.
remove(Object ob)
import java.util.ArrayList;
public class RemoveExample2 {
public static void main(String[] args) {
//Declaring an ArrayList
ArrayList<String> array1 = new ArrayList<String>();
// Use the add() method
array1.add("Python");
array1.add("C++");
array1.add("Javascript");
array1.add("Ruby");
array1.add("Java");
//Use the size() method of Java
System.out.println("The Size of the ArrayList is: " + array1.size());
//Print all the elements of the ArrayList
for (String pl : array1) {
System.out.println("Programming Language is: " + pl);
}
// Remove the element at Index 3
array1.remove("Ruby");
System.out.println("\nThe Size of the ArrayList after removing the element is : " + array1.size());
// Print all the elements specified in the ArrayList
for (String pl : array1) {
System.out.println("Programming Language is: " + pl);
}
}
}
In the above Java code, the only difference appears when declaring the remove() method that takes an object “Ruby” to be removed from the ArrayList.
Output
The output below shows that the size of the ArrayList is reduced to “4” and the object “Ruby” at index “3” is removed.

Method 2: Removing the Element Using the removeIf() Method
Let us suppose we want to remove elements from the List that contain a specified character, for this purpose, we will use the removeIf() method of Java. It uses a predicate filter(that filters the ArrayList) as an argument and removes the element from the ArrayList, as described in the code below.
import java.util.ArrayList;
public class RemoveMethod2 { //main() method
public static void main(String[] args) {
//Declaring an ArrayList
ArrayList<String> array1 = new ArrayList<String>();
// Use the add() method
array1.add("Python");
array1.add("C++");
array1.add("Javascript");
array1.add("Ruby");
array1.add("Java");
//Use the size() method of Java
System.out.println("The Size of the ArrayList is: " + array1.size());
//Print all the elements of the ArrayList
for (String pl : array1) {
System.out.println("Programming Languages are: " + pl);
}
// Remove the element that contains the character “a” at index 1.
array1.removeIf(n -> (n.charAt(1) == 'a'));
System.out.println("\nAfter removing the element the size of the ArrayList is: " + array1);
// Printing the elements present in the specified ArrayList
for (String pl : array1) {
System.out.println("Programming Languages are: " + pl);
}
}
}
In the above Java code, the removeIf() method filters the ArrayList using the specified character “a” with the index “1”. Thus all the elements that contain the character “a” at index “1” are deleted.
Output
In the below output, the size of the ArrayList is reduced to “3” since the elements “Java” and “Javascript” contain the character “a” at index “1”.

Method 3: ListIterator.remove() Method
The “ListIterator.remove()” method in Java removes the specified element from the ArrayList using the Iterator method that is ListIterator.next() which iterates over the ArrayList and returns the next element. When the element to be removed is found the loop breaks. The ListItertor.remove() method of Java is implemented below.
//Import the required Packagesimport java.util.ListIterator;
import java.util.ArrayList;
public class RemoveMethod3{
public static void main(String[] args)
{
ArrayList<String> array1 = new ArrayList<String>();
// Use the add() method
array1.add("Python");
array1.add("C++");
array1.add("Javascript");
array1.add("Ruby");
array1.add("Java");
//List iterator method
ListIterator<String> lit= array1.listIterator();
System.out.println("The Size of the ArrayList is: "+ array1);
for (String st : array1) {
lit.next();
//The loop stops at the element to be removed
if (st == "Ruby")
break;
}
lit.remove();
System.out.println("Modified ArrayList After Removal of an Element is: "+ array1);
}
}
In the above Java code,
- The Java object “lit” is created for the listIterator() method.
- The for loop in Java iterates over the elements present in the ArrayList with the lit.next() method and the if statement is declared to remove the element “Ruby”.
Output
The output below depicts that the original ArrayList contains all the elements whereas the modified ArrayList consists of four elements since “Ruby” from the third index is removed.

Method 4: Using the valueOf() Method to Remove the Element From the ArrayList
The Integer.valueOf() method of Java returns an instance of an integer with the specified integer value. Here’s a code example that utilizes the “integer.valueOf()” method in Java:
import java.util.ArrayList;
//Declare the Class
public class RemoveMethod4 {
public static void main(String[] args) {
//Declaring an ArrayList
ArrayList<Integer> array1 = new ArrayList<Integer>();
// Use the add() method
array1.add(22);
array1.add(12);
array1.add(17);
array1.add(15);
//Use the size() method of Java
System.out.println("The Size of the ArrayList is: " + array1.size());
array1.remove(Integer.valueOf(15));
System.out.println("After Removing the Element the ArrayList is: " +array1);
}
}
The above code implements the remove method along with the Integer.valueOf() method to delete the required element from the ArrayList.
Output

Exception Raised in Java While Removing the Element
When the index provided exceeds the limit of the elements present in the ArrayList, an exception is raised that is termed “IndexOutOfBoundsException”. This exception in Java depicts that the declared index is out of bounds from the specified length of the ArrayList.
import java.util.ArrayList;
//Declare the Class
public class RemoveMethod5 {
public static void main(String[] args) {
//Declaring an ArrayList
ArrayList<String> array1 = new ArrayList<String>();
// Use the add() method
array1.add("Program");
array1.add("in");
array1.add("Java");
//Use the size() method of Java
System.out.println("The Size of the ArrayList is: " + array1.size());
array1.remove(5);
System.out.println("After Removing the Element the ArrayList is: " +array1);
}
}
The above code tends to remove the element at index “5” whereas, the total length of the ArrayList is “3”.
Output
Since the index exceeds the specified length of the ArrayList, the “IndexOutOfBoundsException” exception is raised.

This concludes the implementation of different methods along with the exception raised when removing the element from the ArrayList in Java.
Conclusion
An element from the ArrayList in Java is removed using different methods, such as remove(), removeIf(), valueOf(), and listIterator.remove() method. The most simple and quick way to delete the element from the ArrayList is by using the remove() method. In this article, we have described various methods to delete an element from the ArrayList in Java.