ArrayList remove() method in Java

The remove() method of the ArrayList class helps remove the specified element or element present at a specified index. There are two overloaded implementations of the remove() method.

Let’s have a look at these methods one by one.

public E remove(int index)

  • What does it do? It will remove the element present at the index passed in the arguments of the function and then shift the subsequent elements to the left.
  • What does it return? It will return the removed element from the ArrayList

Remember, the indexing is 0-index. So, passing 1 as an index in the method’s argument will remove the second element from the list.

Code example
public class Codekru {

	public static void main(String[] args) throws Exception {

		ArrayList<String> al = new ArrayList<String>();
		al.add("first");
		al.add("second");
		al.add("third");

		System.out.println("ArrayList contents before: " + al.toString());
		System.out.println(al.remove(1));
		System.out.println("ArrayList contents after: " + al.toString());

	}
}

Output –

ArrayList contents before: [first, second, third]
second
ArrayList contents after: [first, third]
What if we pass the index < 0 or index >= size of the ArrayList?

In this case, the remove() method will throw IndexOutOfBoundsException as illustrated by the below program.

public class Codekru {

	public static void main(String[] args) throws Exception {

		ArrayList<String> al = new ArrayList<String>();
		al.add("first");
		al.add("second");
		al.add("third");

		al.remove(-1);

	}
}

Output –

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 3
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
	at java.base/java.util.Objects.checkIndex(Objects.java:359)
	at java.base/java.util.ArrayList.remove(ArrayList.java:504)
What if use the remove() method on a null ArrayList?

It will throw NullPointerException.

public class Codekru {

	public static void main(String[] args) throws Exception {

		ArrayList<String> al = new ArrayList<String>();
		al = null;

		al.remove(1);

	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.ArrayList.remove(int)" because "al" is null

public boolean remove(Object o)

  • What does it do? It will remove the first occurrence of the specified element from the ArrayList
  • What does it return? It will return true if the element is removed from the list. Otherwise, it will return false.
Code Example
public class Codekru {

	public static void main(String[] args) throws Exception {

		ArrayList<String> al = new ArrayList<String>();
		al.add("first");
		al.add("second");
		al.add("third");

		System.out.println("ArrayList contents: " + al);
		System.out.println("element removed? " + al.remove("first"));
		System.out.println("ArrayList contents: " + al);

		System.out.println("element removed? " + al.remove("sixth"));
		System.out.println("ArrayList contents: " + al);

	}
}

Output –

ArrayList contents: [first, second, third]
element removed? true
ArrayList contents: [second, third]
element removed? false
ArrayList contents: [second, third]
Can we also remove the null objects using the remove() method?

Yes, we can remove the null objects also by using the remove() method as illustrated in the below program.

public class Codekru {

	public static void main(String[] args) throws Exception {

		ArrayList<String> al = new ArrayList<String>();
		al.add("first");
		al.add(null);
		al.add("second");
		al.add(null);
		al.add("third");

		System.out.println("ArrayList contents before: " + al);
		System.out.println("element removed? " + al.remove(null));
		System.out.println("ArrayList contents after: " + al);

	}
}

Output –

ArrayList contents before: [first, null, second, null, third]
element removed? true
ArrayList contents after: [first, second, null, third]

The null object was present at two different positions. So, the remove() method only removed the first occurrence of the null object.

Time complexity of the remove() method

The average case time complexity of the remove() method would be O(n).

Please visit this link to learn more about the ArrayList class of java and its other functions or methods.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Related Article –
Liked the article? Share this on

Leave a Comment

Your email address will not be published.