Java ArrayList remove() Method
The remove()
method is used to delete a single element from the dynamic array.
The syntax for the remove()
method is:
// Remove specified element
arraylist.remove(Object obj)
// Remove element at specified index
arraylist.remove(int index)
Note: arraylist
is an object of the ArrayList
class.
Parameter Description:
obj
- The element to be removedindex
- The index of the element to be removed
If the obj
element appears multiple times, the first occurrence in the dynamic array is removed.
Return Value
If an element is passed, it returns true
if the element is successfully removed.
If an index is passed, it returns the removed element.
Note: If the specified index is out of range, this method throws an IndexOutOfBoundsException
.
Example
Removing a specified element from the array:
Example
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
// Create an array
ArrayList<String> sites = new ArrayList<>();
sites.add("Google");
sites.add("tutorialpro");
sites.add("Taobao");
System.out.println("Website List: " + sites);
// Remove element Taobao
boolean result = sites.remove("Taobao");
System.out.println("Is Taobao deleted? " + result);
System.out.println("After using remove(): " + sites);
}
}
Output:
Website List: [Google, tutorialpro, Taobao]
Is Taobao deleted? true
After using remove(): [Google, tutorialpro]
In the above example, we used the remove()
method to delete "Taobao" from the dynamic array.
Removing an element from a specified position:
Example
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
// Create an array
ArrayList<String> sites = new ArrayList<>();
sites.add("Google");
sites.add("tutorialpro");
sites.add("Taobao");
System.out.println("Website List: " + sites);
// Remove element at index 2
String element = sites.remove(2);
System.out.println("After using remove(): " + sites);
System.out.println("Removed element: " + element);
}
}
Output:
Website List: [Google, tutorialpro, Taobao]
After using remove(): [Google, tutorialpro]
Removed element: Taobao
Removing the first occurrence of an element:
Example
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
// Create an array
ArrayList<Integer> randomNumbers = new ArrayList<>();
// Insert elements into the array
randomNumbers.add(22);
randomNumbers.add(13);
randomNumbers.add(35);
randomNumbers.add(13);
randomNumbers.add(40);
// Remove the first occurrence of 13
boolean result = randomNumbers.remove(Integer.valueOf(13));
System.out.println("Is 13 deleted? " + result);
System.out.println("After using remove(): " + randomNumbers);
}
}
Output:
Is 13 deleted? true
After using remove(): [22, 35, 13, 40]
System.out.println("ArrayList: " + randomNumbers);
// Remove the first occurrence of the element 13
boolean result = randomNumbers.remove(Integer.valueOf(13));
System.out.println("Was 13 removed? " + result);
System.out.println("After using remove(): " + randomNumbers);
}
}
The output of the above program is:
ArrayList: [22, 13, 35, 13, 40]
Was 13 removed? true
After using remove(): [22, 35, 13, 40]
In the example above, we created a dynamic array named randomNumbers. In this array, the element 13 appears twice. Note this line:
randomNumbers.remove(Integer.valueOf(13))
Integer.valueOf()
converts 13 from an int type to an Integer object. Since the remove() method only takes an object as its parameter.
remove()
is used to remove the first occurrence of 13 in the array.
Note: We can also use the clear() method to remove all elements from the array. For more information, visit Java ArrayList clear().