92
Java ArrayList remove() method
The remove() method of Java ArrayList class removes the first matching object in the ArrayList.
Syntax:
Parameter:
“object”:It is the ArrayList element that will be removed if exist.
Return:
Return “true”: If this list contained the specified object.
Example 1
Output:
[A, B, C, D, E] true [A, B, D, E]
Example 2
Output:
[Parrot, Owl, Swallow] false [Parrot, Owl, Swallow]
Java ArrayList remove(int index) method
The remove (int index) method of Java ArrayListclass removes an element of specified index of the ArrayList.
Syntax:
Parameter:
“index”: index of the element that will be removed.
Return:
Return “E”: the element that was removed from the list.
Example 3
Output:
[]
Example 4
Output:
[1]
Example 5
Output:
throwsIndexOutOfBoundsException
Next TopicJava ArrayList