Java ArrayList remove() Method

Java ArrayList Remove Method

What is an ArrayList?

ArrayList provides us with dynamic arrays. The ArrayLists are resizable in nature , we can add or remove an element at anytime. It is not compulsory to mention size while declaring it. The ArrayList  is a part of Java framework and can be found in the java.util pacakge. ArrayList have many inbuilt methods which we can use. One of them is Java ArrayList remove() Method.

Here in the page we will discuus about the remove() method.

Java ArrayList remove() Method

In Java ArrayList class many inbuilt methods are their which can be used to perform different task on them.  remove() method is one of them , which is used to remove an element from the ArrayList.
the ArrayList remove() method. It’s parameter, uses , output are discussed below with examples.

Syntax :

ArrayList.remove(index/ element)

Parameters :

ArrayList: Name of the arraylist from which the element has to be removed.
index: Index of the element which has to be removed from the list.
Element: Element in the list which has to be removed.

Returns :

 If index is passed as parameter , it removes the element from the specified position
 If object is passed as parameter , it removes the first occurrence of the object and returns true
 if object is present in the list.
ArrayList-remove-method

ArrayList remove() Method Examples

Example 1:

When index is passed as parameter.

Run

package com.company;
import java.util.ArrayList;
// Main class
public class prepInsta
{
  // Main driver method
  public static void main (String[]args)
  {
    //Creating an Integer ArrayList
    List < Integer > list = new ArrayList <> ();

    // adding elements in the list
    list.add (1);
    list.add (2);
    list.add (3);
    list.add (11);
    list.add (21);

    // calling remove() method using index
    list.remove (1);

    list.remove (1);

    // Printing the updated ArrayList
    System.out.println (list);
  }
}

Output:

[1, 11, 21]

Example 2:

When value is passed as parameter OR user wants to remove the first occurrence of the element in the list.

Run

package com.company;
import java.util.ArrayList;

// Main class
public class prepInsta
{
  // Main driver method
  public static void main (String[]args)
  {
    //Creating an Integer ArrayList
    List < Integer > list = new ArrayList <> ();

    // adding elements in the list
    list.add (1);
    list.add (2);
    list.add (3);
    list.add (11);
    list.add (21);

    // calling remove() method using index
    list.remove (Integer.valueOf (2));

    list.remove (Integer.valueOf (11));

    // Printing the updated ArrayList
    System.out.println (list);
  }
}

Output:

[1, 3, 21]

Example 3:

When user wants to remove specified element from the list.

Run

package com.company;
import java.util.ArrayList;

class PrepInsta {
    public static void main(String[] args) {
        // creating an String ArrayList
        ArrayList list = new ArrayList<>();

        // insert element to the arraylist
        list.add("Prep");
        list.add("Insta");
        list.add("Prime");

        // remove the element Prime from the list
        boolean result = list.remove("Prime");

        System.out.println("Is element Java removed? " + result);
        System.out.println(list);
    }
}

Output:

Is element Java removed? true
[Prep, Insta]

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription