The remove()
method removes the single element from the arraylist.
Example
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
// create an ArrayList
ArrayList<Integer> primeNumbers = new ArrayList<>();
primeNumbers.add(2);
primeNumbers.add(3);
primeNumbers.add(5);
System.out.println("ArrayList: " + primeNumbers);
// remove element at index 2
int removedElement = primeNumbers.remove(2);
System.out.println("Removed Element: " + removedElement);
}
}
// Output: ArrayList: [2, 3, 5]
// Removed Element: 5
Syntax of ArrayList remove()
The syntax of the remove()
method is:
// remove the specified element
arraylist.remove(Object obj)
// remove element present in the specified index
arraylist.remove(int index)
Here, arraylist is an object of the ArrayList
class.
remove() Parameters
The remove()
method takes a single parameter.
- obj - element that is to be removed from the arraylist, OR
- index - position from where element is to be removed
If the same element obj is present in multiple location, then the element that appear first in the arraylist is removed.
remove() Return Value
- returns true if specified element is present in the arraylist
- returns the removed element if index is passed as parameter
Note: If the specified index is out of range, the method throws IndexOutOfBoundsException
.
Example 1: Remove the Specified Element from the ArrayList
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
// create an ArrayList
ArrayList<String> languages = new ArrayList<>();
// insert element to the arraylist
languages.add("JavaScript");
languages.add("Java");
languages.add("Python");
System.out.println("ArrayList: " + languages);
// remove the element Java
boolean result = languages.remove("Java");
System.out.println("Is element Java removed? " + result);
System.out.println("ArrayList after remove(): " + languages);
}
}
Output
ArrayList: [JavaScript, Java, Python] Is element Java removed? true ArrayList after remove(): [JavaScript, Python]
In the above example, we have created a arraylist named languages. The arraylist stores the name of programming languages.
Here, we have used the remove()
method to remove the element Java from the arraylist.
Example 2: Remove the Element From the Specified Position
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
// create an ArrayList
ArrayList<String> languages = new ArrayList<>();
// insert element to the arraylist
languages.add("JavaScript");
languages.add("Java");
languages.add("Python");
System.out.println("ArrayList: " + languages);
// remove the element from position 2
String element = languages.remove(2);
System.out.println("ArrayList after remove(): " + languages);
System.out.println("Removed Element: " + element);
}
}
Output
ArrayList: [JavaScript, Java, Python] ArrayList after remove(): [JavaScript, Java] Removed Element: Python
In the above example, we have created an arraylist named languages. Notice the expression,
languages.remove(2)
Here, the remove() returns and removes the element present at position 2 (i.e. Python).
Example 3: Remove the First Occurrence of the Element
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
// create an ArrayList
ArrayList<Integer> randomNumbers = new ArrayList<>();
// add element to the arraylist
randomNumbers.add(22);
randomNumbers.add(13);
randomNumbers.add(35);
randomNumbers.add(13);
randomNumbers.add(40);
System.out.println("ArrayList: " + randomNumbers);
// remove the first occurrence of 13
boolean result = randomNumbers.remove(Integer.valueOf(13));
System.out.println("Is element 13 removed? " + result);
System.out.println("ArrayList after remove(): " + randomNumbers);
}
}
Output
ArrayList: [22, 13, 35, 13, 40] Is element 13 removed? true ArrayList after remove(): [22, 35, 13, 40]
In the above example, we have created an arraylists named randomNumbers . In the arraylist, the element 13 is present in two locations. Notice the line,
randomNumbers.remove(Integer.valueOf(13))
Here,
Integer.valueOf()
- Converts theint
value 13 to anInteger
object. It is because theremove()
method only takes objects as its arguments. To learn more, visit Java Primitive Types to Wrapper Objects.remove()
- Removes the element 13 that appeared first in the arraylist.
Note: We can also remove all the elements from the arraylist using the clear()
method. To learn more, visit Java ArrayList clear().