Home » Java programming language

Java ArrayList remove() Method with Example

ArrayList Class remove() method: Here, we are going to learn about the remove() method of ArrayList Class with its syntax and example.
Submitted by Preeti Jain, on January 18, 2020

ArrayList Class remove() method

Syntax:

    public boolean remove(Object obj);
    public T remove(int indices);
  • remove() method is available in java.util package.
  • remove(Object obj) method is used to delete the first occurrence of the given object from this Arraylist when exists.
  • remove(int indices) method is used to delete the element at the given indices from this Arraylist & shift other elements to the left side.
  • remove(Object obj) method does not throw an exception at the time of removing an element.
  • remove(obj int indices) method may throw an exception at the time of removing an element at the given indices.
    IndexOutOfBoundsException: This exception may throw when the given indices are not in a range.
  • These are non-static methods, it is accessible with class object & if we try to access these methods with the class name then we will get an error.

Parameter(s):

  • In the first case, remove(Object obj)
    • Object obj – represents the object to be removed from this Arraylist when exists.
  • In the second case, remove (int indices)
    • int indices – represent the indices of the element to be removed from this Arraylist.

Return value:

In the first case, the return type of the method is boolean, it returns true if the given object is to be removed from the Arraylist when exists.

In the second case, the return type of the method is T, it returns the removed element from this Arraylist.

Example:

// Java program to demonstrate the example 
// of remove() method of ArrayList.

import java.util.*;

public class RemoveOfArrayList {
    public static void main(String[] args) {
        // Create an ArrayList with initial 
        // capacity of storing elements

        ArrayList < String > arr_l = new ArrayList < String > (10);

        // By using add() method is to add 
        // elements in this ArrayList

        arr_l.add("C");
        arr_l.add("C++");
        arr_l.add("JAVA");
        arr_l.add("DOTNET");
        arr_l.add("PHP");

        // Display ArrayList 
        System.out.println("arr_l :" + arr_l);

        // By using remove(Object) method is to remove
        // the given object from this ArrayList
        arr_l.remove("C++");

        // Display ArrayList 
        System.out.println("arr_l.remove(C++) :" + arr_l);

        // By using remove(int) method is to remove
        // the object at the given index from this ArrayList
        arr_l.remove(3);

        // Display ArrayList 
        System.out.println("arr_l.remove(3) : " + arr_l);
    }
}

Output

arr_l :[C, C++, JAVA, DOTNET, PHP]
arr_l.remove(C++) :[C, JAVA, DOTNET, PHP]
arr_l.remove(3) : [C, JAVA, DOTNET]


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.