ArrayList.remove(Object) in Java

ArrayList.remove(Object): This method is available in java.util.ArrayList class of Java.

Syntax:

boolean java.util.ArrayList.remove(Object o)

This method takes one argument of type Object as its parameter. This method removes the first occurrence of the specified element from this list,if it is present. If the list does not contain the element, it is unchanged.

Parameters: One parameter is required for this method.

o: element to be removed from this list, if present.

Returns: true if this list contained the specified element.

Exceptions: NA

Approach

Java

import java.util.ArrayList;

public class ArrayListRemoveObject {
    public static void main(String[] args) {
        ArrayList<Integer> arr = new ArrayList<>();
        arr.add(1);
        arr.add(2);
        Integer i = 2;
        System.out.println(arr.remove(i));

    }
}

Output:

true


No comments:

Post a Comment