ArrayList remove() Method in Java

Published by user on

The remove() method of ArrayList removes the specified element from the list. We can also remove an element at a particular position using remove(int index). It throws IndexOutOfBoundsException if the index is out of range.

Syntax:

public boolean remove(Object o)

Parameters – o is the element to be removed.
Returns – true on successful removal of the element otherwise false if the element is not found in the list.

public E remove(int index)

Parameters – index is the position of the element to be removed.
It returns the removed element.

Remove element from ArrayList

The below program shows how to use the remove(Element e) method to remove the specified element from the list.

import java.util.ArrayList;

public class ArrayListRemoveExample {

	public static void main(String[] args) {
		ArrayList<String> employees = new ArrayList<>();
		employees.add("Adam");
		employees.add("Ricky");
		employees.add("Chris");
		employees.add("John");
		employees.add("Steve");

		System.out.println("ArrayList element: " + employees);
		employees.remove("Chris");
		System.out.println("ArrayList elements after removing Chris: " + employees);
	}
}

Output:

ArrayList element: [Adam, Ricky, Chris, John, Steve]
ArrayList elements after removing Chris: [Adam, Ricky, John, Steve]

Remove element at the specified index

Let’s see how to use the remove(int index) method to remove the element at the specified index from the list.

import java.util.ArrayList;

public class ArrayListRemoveWithIndex {

	public static void main(String[] args) {
		ArrayList<Integer> numbers = new ArrayList<>();
		numbers.add(11);
		numbers.add(17);
		numbers.add(33);
		numbers.add(45);
		numbers.add(67);

		System.out.println("ArrayList element : " + numbers);
		numbers.remove(2);
		System.out.println("ArrayList after removing element index 2 : " + numbers);
	}
}

Output:

ArrayList element : [11, 17, 33, 45, 67]
ArrayList after removing element index 2 : [11, 17, 45, 67]

Scenario when the index is Out of Range

Let’s look at how remove() method throws IndexOutOfBoundsException if the index is out of range.

import java.util.ArrayList;

public class ArrayListRemoveIndexOutOfBound {

	public static void main(String[] args) {
		ArrayList<String> countries = new ArrayList<>();
		countries.add("India");
		countries.add("Nepal");
		countries.add("Srilanka");
		countries.add("Australia");

		System.out.println("ArrayList element: " + countries);
		countries.remove(8);// throws IndexOutOfBoundsException
	}
}

Output:

ArrayList element: [India, Nepal, Srilanka, Australia]
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 8, Size: 4
	at java.util.ArrayList.rangeCheck(ArrayList.java:653)
	at java.util.ArrayList.remove(ArrayList.java:492)
	at ArrayListRemoveIndexOutOfBound.main(ArrayListRemoveIndexOutOfBound.java:13)

That’s all about this article. Please share it if you found it helpful.

Further References:

ArrayList Documentation

Categories: Java