List interface in Java (which is implemented by ArrayList and LinkedList) provides two versions of remove method.
boolean remove(Obejct 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
// 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,
// 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.
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.
Recommended Posts:
- ArrayList vs LinkedList in Java
- Program to convert ArrayList to LinkedList in Java
- ArrayList of ArrayList in Java
- ArrayList to Array Conversion in Java : toArray() Methods
- LinkedList remove() Method in Java
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- How to remove an element from ArrayList in Java?
- Remove all elements from the ArrayList in Java
- Remove repeated elements from ArrayList in Java
- How to Remove Duplicates from ArrayList in Java
- Remove first element from ArrayList in Java
- Static methods vs Instance methods in Java
- LinkedList descendingIterator() method in Java with Examples
- LinkedList element() method in Java with Examples
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
- Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
- Java.util.LinkedList.get(), getFirst(), getLast() in Java
- Java.util.LinkedList.indexOf(), lastIndexof() in Java
- Difference Between LinkedList and LinkedHashSet in Java
- ArrayList toArray() method in Java with Examples
Improved By : dhruvagni95

