How to remove elements from ArrayList in Java?

In this Java ArrayList tutorial, you will learn how to remove elements from ArrayList in Java. There are actually two methods to remove an existing element from ArrayList, first by using the remove(int index) method, which removes elements with given index, remember index starts with zero in ArrayList. So a call to remove(2) in an ArrayList of {"one", "two", "three"} will remove 3rd element which is "three". The second method to remove element is remove(Object obj), which removes given object from ArrayList. For example, a call to remove("two") will remove the second element from ArrayList. Though you should remember to use Iterator or ListIterator remove() method to delete elements while iterating, using ArrayList's remove methods, in that case, will throw ConcurrentModificationException in Java.

Things get little complicated when you are working with ArrayList of integral numbers e.g. ArrayList of integers. If your list contains numbers which are same as indexes e.g. then a call to remove(int index) can be confused with a call to remove(Object obj).

For example, if you have an ArrayList containing {1, 2, 3} then a call to remove(2) is ambiguous, because it could be interpreted a call to remove 2, which is second element or a call to remove element at index 2, which is actually 3.

If you want to learn more about Collection classes, I suggest you to take a look at one of the all time classic book, Java Generics and Collection. This is the book I refer to refresh my knowledge on the topic.




Java ArrayList Remove Object Example

Here is the Java program to remove a given object from ArrayList in Java. In this example, I have used the second remove method, which deletes the given object from ArrayList. Anyway, since we have an ArrayList of String, you can use any of those method, its safe. You can also check size() of ArrayList before and after removing elements.

Remember, size() method gives total number of elements in ArrayList, so it should reduce by one. This is different than the size of array which is backing ArrayList, which will remain same. In our example, you can see that number of elements are gradually reduced after removal.

You can also see Core Java Volume 1 - Fundamentals by Cay S. Horstmann to learn more about how to remove objects from ArrayList in Java. One of the most comprehensive yet easy to read book on Java programming.

How to remove objects from ArrayList in Java


Java Program to remove given element from ArrayList
import java.util.ArrayList;

/**
 * Java program to remove an element from ArrayList
 *
 * @author WINDOWS 8
 */

public class ArrayListRemoveDemo{

    public static void main(String args[]) {

        ArrayList<String> cities = new ArrayList<>();
        cities.add("London");
        cities.add("Tokyo");
        cities.add("HongKong");
        cities.add("NewYork");
        cities.add("Berlin");
       
       
        System.out.println("Before removing any element from ArrayList : " + cities);
        cities.remove("London");
       
        System.out.println("After removing one element from ArrayList : " + cities);
        cities.remove("Tokyo");
       
        System.out.println("After removing two objects from ArrayList : " + cities);
     

    }

}

Output
Before removing any element from ArrayList : [London, Tokyo, HongKong, NewYork, Berlin]
After removing one element from ArrayList : [Tokyo, HongKong, NewYork, Berlin]
After removing two objects from ArrayList : [HongKong, NewYork, Berlin]

Here is another Java example to remove an object at given index form ArrayList  in Java.

How to remove elements form ArrayList in Java


That's all about how to take out an object from ArrayList in Java. Remember there are two methods to remove elements, first remove(int index) and second remove(Object obj), if you are working with ArrayList of Integer, Short, Byte or Long then autoboxing may cause problem for you, because a call to remove(1) may be interpreted as call to remove(index) to remove second element or a call to remove(Object) to remove element with value 1. This is why you should take care while overloading method with same number of argument, you can avoid this kind of ambiguity by following some overloading best practices.


1 comment:

  1. See here also to learn more about how to remove elements from an ArrayList.

    ReplyDelete