In this tutorial, we will learn about the Java ArrayList remove() method, and learn how to use this method to remove an element from the ArrayList at a specific index or by object, with the help of examples.

Java ArrayList remove(index) method

ArrayList remove() removes the element at the specified position in this ArrayList, and returns the removed object.

Syntax

The syntax of remove() method with index as argument is

</>
Copy
ArrayList.remove(int index)

where

ParameterDescription
indexThe index of the element to be removed from this ArrayList.

Returns

The method returns an object of the type of elements in the ArrayList.

1. Example for ArrayList remove(int index)

In this example, we will define an ArrayList of Strings and initialize it with some elements in it. We will use ArrayList remove(index) method to remove the element present at index 2 in this ArrayList.

Java Program

</>
Copy
import java.util.ArrayList;

public class Example {
	public static void main(String[] args) {
		ArrayList<String> arrayList = new ArrayList<String>();  
		arrayList.add("a");  
		arrayList.add("b");  
		arrayList.add("c"); 
		arrayList.add("d");
		System.out.println("Original ArrayList       : " + arrayList);
		
		int index = 2;
		String obj = arrayList.remove(index);
		System.out.println("Object removed : " + obj);
		System.out.println("ArrayList after remove() : " + arrayList);
	}
}

Output

Original ArrayList       : [a, b, c, d]
Object removed : c
ArrayList after remove() : [a, b, d]

2. remove(int index) – When given index is out of bounds for the ArrayList

In this example, we will define an ArrayList of Strings and initialize it with four elements in it. We will use ArrayList remove(index) method to remove the element present at index 8 in this ArrayList. Since the index 8 is out of bounds for the ArrayList, remove() method throws java.lang.IndexOutOfBoundsException.

Java Program

</>
Copy
import java.util.ArrayList;

public class Example {
	public static void main(String[] args) {
		ArrayList<String> arrayList = new ArrayList<String>();  
		arrayList.add("a");  
		arrayList.add("b");  
		arrayList.add("c"); 
		arrayList.add("d");
		System.out.println("Original ArrayList       : " + arrayList);
		
		int index = 8;
		arrayList.remove(index);

		System.out.println("ArrayList after remove() : " + arrayList);
	}
}

Output

Original ArrayList       : [a, b, c, d]
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 8 out-of-bounds for length 4
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Unknown Source)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Unknown Source)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Unknown Source)
	at java.base/java.util.Objects.checkIndex(Unknown Source)
	at java.base/java.util.ArrayList.remove(Unknown Source)
	at Example.main(Example.java:13)

Java ArrayList remove(obj)

ArrayList remove() removes the first occurrence of the specified element from this ArrayList, if it is present.

If the object/element is not present, then remove(obj) does nothing.

If the specified object is present and removed, then remove() returns true, else it returns false.

Syntax

The syntax of remove() method with object/element as argument is

</>
Copy
ArrayList.remove(Object obj)

where

ParameterDescription
objThe element to be removed from this ArrayList.

Returns

The method returns boolean value.

3. remove(obj) Example when object to be removed is present in the list

In this example, we will define an ArrayList of Strings and initialize it with some elements in it. We will use ArrayList remove(obj) method to remove the first occurrence of element "c" from this ArrayList.

ArrayList remove(obj) should remove the element and return true.

Java Program

</>
Copy
import java.util.ArrayList;

public class Example {
	public static void main(String[] args) {
		ArrayList<String> arrayList = new ArrayList<String>();  
		arrayList.add("a");  
		arrayList.add("b");  
		arrayList.add("c"); 
		arrayList.add("d");
		System.out.println("Original ArrayList       : " + arrayList);

		Object obj = "c";
		boolean removed = arrayList.remove(obj);
		System.out.println("Is the object removed? " + removed);

		System.out.println("ArrayList after remove() : " + arrayList);
	}
}

Output

Original ArrayList       : [a, b, c, d]
Is the object removed? true
ArrayList after remove() : [a, b, d]

4. remove(obj) Example when object to be removed is not present in the list

In this example, we will define an ArrayList of Strings and initialize it with some elements in it. We will use ArrayList remove(obj) method to remove the first occurrence of element "m" from this ArrayList.

Since, the element "m" is not present in the ArrayList, ArrayList remove(obj) should do nothing and return false.

Java Program

</>
Copy
import java.util.ArrayList;

public class Example {
	public static void main(String[] args) {
		ArrayList<String> arrayList = new ArrayList<String>();  
		arrayList.add("a");  
		arrayList.add("b");  
		arrayList.add("c"); 
		arrayList.add("d");
		System.out.println("Original ArrayList       : " + arrayList);

		Object obj = "m";
		boolean removed = arrayList.remove(obj);
		System.out.println("Is the object removed? " + removed);

		System.out.println("ArrayList after remove() : " + arrayList);
	}
}

Output

Original ArrayList       : [a, b, c, d]
Is the object removed? false
ArrayList after remove() : [a, b, c, d]

Conclusion

In this Java Tutorial, we have learnt the syntax of Java ArrayList remove() method, and also learnt how to use this method with the help of examples.