How to remove an element from ArrayList in Java?

There are two way to remove an element from ArrayList.

1. By using remove() methods :
ArrayList provides two overloaded remove() method.
a. remove(int index) : Accept index of object to be removed.
b. remove(Obejct obj) : Accept object to be removed.

What happens when we have an integer arrayList and we want to remove an item? For example consider below program.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate working of remove
// on an integer arraylist
import java.util.List;
import java.util.ArrayList;
  
public class GFG
{
    public static void main(String[] args)
    {
        List<Integer> al = new ArrayList<>();
        al.add(10);
        al.add(20);
        al.add(30);
        al.add(1);
        al.add(2);
  
        // This makes a call to remove(int) and 
        // removes element 20.
        al.remove(1);
          
        // Now element 30 is moved one position back
        // So element 30 is removed this time
        al.remove(1);
  
        System.out.println("Modified ArrayList : " + al);
    }
}

chevron_right


Output :

Modified ArrayList : [10, 1, 2]

We can see that the passed parameter is considered as index. How to remove elements by value.



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate working of remove
// on an integer arraylist
import java.util.List;
import java.util.ArrayList;
  
public class GFG
{
    public static void main(String[] args)
    {
        List<Integer> al = new ArrayList<>();
        al.add(10);
        al.add(20);
        al.add(30);
        al.add(1);
        al.add(2);
  
        // This makes a call to remove(Object) and 
        // removes element 1
        al.remove(new Integer(1));
          
        // This makes a call to remove(Object) and 
        // removes element 2
        al.remove(new Integer(2));
  
        System.out.println("Modified ArrayList : " + al);
    }
}

chevron_right


Output :

Modified ArrayList : [10, 20, 30]

 

2. Using Iterator.remove() method :
It is not recommended to use ArrayList.remove() when iterating over elements. This may lead to ConcurrentModificationException (Refer this for a sample program with this exception). When iterating over elements, it is recommended to use Iterator.remove() method .

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate working of
// Iterator.remove() on an integer arraylist
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
  
public class GFG
{
    public static void main(String[] args)
    {
        List<Integer> al = new ArrayList<>();
        al.add(10);
        al.add(20);
        al.add(30);
        al.add(1);
        al.add(2);
  
        // Remove elements smaller than 10 using
        // Iterator.remove()
        Iterator itr = al.iterator();
        while (itr.hasNext())
        {
            int x = (Integer)itr.next();
            if (x < 10)
                itr.remove();
        }
  
        System.out.println("Modified ArrayList : "
                                           + al);
    }
}

chevron_right


Output :

Modified ArrayList : [10, 20, 30]

This article is contributed by Nitsdheerendra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Improved By : Ankur Goel