Open In App

ArrayList and LinkedList remove() methods in Java with Examples

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. 

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






// 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. 



It throws IndexOutOfBoundsException if index is out of bound, 




// 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: 


Article Tags :