The Wayback Machine - http://web.archive.org/web/20200420043735/https://www.geeksforgeeks.org/arraylist-linkedlist-remove-methods-java-examples/amp/

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.

remove(Obejct obj) :
It accepts object to be removed.

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



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}
chevron_right

Output:

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


remove(int index) :
It removes the element at given index

It throws IndexOutOfBoundsException if index is out of bound,

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 ArrayList<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); 
    } 
} 
chevron_right

Output:

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

Important Points:

This article is contributed by Mohit Gupta. 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.





Improved By : dhruvagni95

Article Tags :
Practice Tags :