Open In App

ArrayList and LinkedList remove() methods in Java with Examples

Last Updated : 19 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

List interface in Java (which is implemented by ArrayList and LinkedList) provides two versions of remove method. 
boolean remove(Object obj) : 
It accepts object to be removed. It returns true if it finds and removes the element. It returns false if the element to be removed is not present. 

  • Removes the first occurrence of the specified element from given list, if the element is present. If the element is not present, the given list is not changed.
  • After removing, it shifts subsequent elements(if any) to left and decreases their indexes by 1.

It throws following exceptions 
ClassCastException – if the type of the specified element is incompatible with this collection 
(optional). 
NullPointerException – if the specified element is null and this collection does not permit 
null elements(optional). 
UnsupportedOperationException – if the remove operation is not supported by this collection

Java




// A Java program to demonstrate working of list remove
// when Object to be removed is passed.
import java.util.*;
 
public class GFG
{
    public static void main(String[] args)
    {
        // Demonstrating remove on ArrayList
        List<String>  myAlist = new ArrayList<String>();
        myAlist.add("Geeks");
        myAlist.add("Practice");
        myAlist.add("Quiz");
        System.out.println("Original ArrayList : " + myAlist);
        myAlist.remove("Quiz");
        System.out.println("Modified ArrayList : " + myAlist);
 
        // Demonstrating remove on LinkedList
        List<String>  myLlist = new LinkedList<String>();
        myLlist.add("Geeks");
        myLlist.add("Practice");
        myLlist.add("Quiz");
        System.out.println("Original LinkedList : " + myLlist);
        myLlist.remove("Quiz");
        System.out.println("Modified LinkedList : " + myLlist);
    }
}


Output

Original ArrayList : [Geeks, Practice, Quiz]
Modified ArrayList : [Geeks, Practice]
Original LinkedList : [Geeks, Practice, Quiz]
Modified LinkedList : [Geeks, Practice]

object remove(int index) : 
It removes the element at given index. It returns the object that is removed. 

  • After removing, it shifts subsequent elements(if any) to left and decreases their indexes by 1.
  • If the list contains int types, then this method is called when an int is passed (Please refer this for details)

It throws IndexOutOfBoundsException if index is out of bound, 

Java




// A Java program to demonstrate working of list remove
// when index is passed.
import java.util.*;
 
public class GFG
{
    public static void main(String[] args)
    {
        // Demonstrating remove on ArrayList
        List<String> myAlist = new ArrayList<String>();
        myAlist.add("Geeks");
        myAlist.add("Practice");
        myAlist.add("Quiz");
        System.out.println("Original ArrayList : " + myAlist);
        myAlist.remove("Quiz");
        System.out.println("Modified ArrayList : " + myAlist);
 
        // Demonstrating remove on LinkedList
        List<String> myLlist = new LinkedList<String>();
        myLlist.add("Geeks");
        myLlist.add("Practice");
        myLlist.add("Quiz");
        System.out.println("Original LinkedList : " + myLlist);
        myLlist.remove(2);
        System.out.println("Modified LinkedList : " + myLlist);
    }
}


Output

Original ArrayList : [Geeks, Practice, Quiz]
Modified ArrayList : [Geeks, Practice]
Original LinkedList : [Geeks, Practice, Quiz]
Modified LinkedList : [Geeks, Practice]

Important Points: 

  • Note that there is no direct way to remove elements in array as size of array is fixed. So there are no methods like add(), remove(), delete(). But in Collection like ArrayList and Hashset, we have these methods. So it’s better to either convert array to ArrayList or Use Arraylist from first place when we need these methods.
  • It is not recommended to use remove() of list interface 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. Please see this for details.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads