Java ArrayList remove() method
The remove() method is used to delete a single element from the dynamic array.
The syntax of remove() is:
// 删除指定元素 arraylist.remove(Object obj) // 删除指定索引位置的元素 arraylist.remove(int index)
Note:arraylist is an object of the ArrayList class.
Parameter description:
- obj - the element to be deleted
- index - the index value of the element to be deleted
If the obj element appears multiple times, the first occurrence of the element in the dynamic array is deleted.
Return value
If an element is passed and deletion succeeds, returns true.
If an index value is passed, returns the deleted element.
Note: If the specified index is out of range, the method will throw an IndexOutOfBoundsException.
Example
Delete a specified element from the array:
Example
class Main {
public static void main(String[] args){
// create an array
ArrayList<String> sites = new ArrayList<>();
sites.add("Google");
sites.add("Example");
sites.add("Taobao");
System.out.println("Website list: " + sites);
// delete element Taobao
boolean result = sites.remove("Taobao");
System.out.println("Is Taobao deleted? " + result);
System.out.println("After using remove(): " + sites);
}
}
The output result of running the above program is:
网站列表: [Google, Example, Taobao] Taoabo 是否被删除? true 使用 remove() 后: [Google, Example]
In the above example, we used the remove() method to delete Taobao from the dynamic array.
Delete an element from a specified position:
Example
class Main {
public static void main(String[] args){
// create an array
ArrayList<String> sites = new ArrayList<>();
sites.add("Google");
sites.add("Example");
sites.add("Taobao");
System.out.println("Website list: " + sites);
// delete the element at index 2
String element = sites.remove(2);
System.out.println("After using remove(): " + sites);
System.out.println("Removed element: " + element);
}
}
The output result of running the above program is:
网站列表: [Google, Example, Taobao] 使用 remove() 后: [Google, Example] 移除的元素: Taobao
Delete the first occurrence of an element:
Example
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);
System.out.println("ArrayList: " + randomNumbers);
// delete the first occurrence of element 13
boolean result = randomNumbers.remove(Integer.valueOf(13));
System.out.println("Is 13 deleted? " + result);
System.out.println("After using remove(): " + randomNumbers);
}
}
The output result of running the above program is:
ArrayList: [22, 13, 35, 13, 40] 13 是否被删除? true 使用 remove() 后: [22, 35, 13, 40]
In the above example, we created a dynamic array named randomNumbers. In this array, element 13 appears twice. Note this line:
randomNumbers.remove(Integer.valueOf(13))
Integer.valueOf()Convert 13 from int type to an Integer object. Because the remove() method only accepts objects as its parameter.
remove()Used to delete the first 13 that appears in the array.
Note: We can also use the clear() method to delete all elements in the array. To learn more, please visitJava ArrayList clear()。
Other extensions
Java ArrayList