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.
- 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
// 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]
remove(int index) :
It removes the element at given index
- 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,
// 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); } } |
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 ArrrayList 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.
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.
Recommended Posts:
- ArrayList vs LinkedList in Java
- Program to convert ArrayList to LinkedList in Java
- LinkedList remove() Method in Java
- ArrayList to Array Conversion in Java : toArray() Methods
- How to remove an element from ArrayList in Java?
- How to Remove Duplicates from ArrayList in Java
- Remove all elements from the ArrayList in Java
- Remove first element from ArrayList in Java
- Remove repeated elements from ArrayList in Java
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- LinkedList element() method in Java with Examples
- LinkedList descendingIterator() method in Java with Examples
- ArrayList clear() in Java with examples
- Arraylist removeRange() in Java with examples
- ArrayList set() method in Java with Examples
Improved By : dhruvagni95

