Delete elements from a list.
import public |
Definition and Usage
The remove() method eliminates an item from the list, either by its position or its value. When removing by position, this method returns the removed item. When removing by value, it returns true if the value is found and false otherwise.
If multiple elements in the list have the same value, only the first occurrence is deleted.
If the list contains integers and you aim to delete an integer based on its value, you need to pass an Integer object. See More Examples below for a demonstration.
Syntax
One of the following:
public T remove(int index) |
public boolean remove(Object item) |
T denotes to the data type of items within the list.
Parameter Values
Parameter |
Description |
index |
Required. The position of the item to be deleted. |
item |
Required. The value of the item to be deleted. |
Technical Details
Returns: |
If an object was passed as an argument then it returns true if the object was found in the list and false otherwise. If an index was passed then it returns the object which was removed. |
Throws: |
IndexOutOfBoundsException – If the index is less than zero, equal to the size of the list or greater than the size of the list. |
Eliminate an integer from the list using both its position and its value.
import |