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

Test it Now

Output:

[A, B, C, D, E]

true

[A, B, D, E]

Example 2

Test it Now

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

Test it Now

Output:

[]

Example 4

Test it Now

Output:

[1]

Example 5

Test it Now

Output:

throwsIndexOutOfBoundsException

Next TopicJava ArrayList




Latest Courses