Java ArrayList remove() Method



Description

The Java ArrayList remove(int index) method removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

Declaration

Following is the declaration for java.util.ArrayList.remove() method

public E remove(int index)

Parameters

index − The index of the element to be removed .

Return Value

This method returns the element that was removed from the list .

Exception

IndexOutOfBoundsException − if the index is out of range.

Java ArrayList remove(object) Method

Description

The Java ArrayList remove(Object) 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.

Declaration

Following is the declaration for java.util.ArrayList.remove() method

public boolean remove(Object o)

Parameters

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

Return Value

This method returns true if this list contained the specified element, else the list is unchanged.

Exception

NA

Example 1

The following example shows the usage of Java ArrayList remove(index) method. We're creating a ArrayList of Integers. We're adding couple of Integers to the ArrayList object using add() method calls per element. Array size is printed, array is printed and using remove(index) method, an element is removed. Then size and array is printed again.

package com.tutorialspoint;
import java.util.ArrayList;
public class ArrayListDemo {
   public static void main(String[] args) {
      
      // create an empty array list
      ArrayList<Integer> arrayList = new ArrayList<>();

      // use add() method to add elements in the arrayList
      arrayList.add(20);
      arrayList.add(15);
      arrayList.add(30);
      arrayList.add(45);

      System.out.println("Size of list: " + arrayList.size());
	   // let us print all the elements available in list again
      System.out.println("ArrayList = " + arrayList);
      
      // Removes element at 3rd position
      arrayList.remove(2);
      System.out.println("Now, Size of list: " + arrayList.size());
      
      // let us print all the elements available in list again
      System.out.println("ArrayList = " + arrayList);
   }
}   

Output

Let us compile and run the above program, this will produce the following result −

Size of list: 4
ArrayList = [20, 15, 30, 45]
Now, Size of list: 3
ArrayList = [20, 15, 45]

Example 2

The following example shows the usage of Java ArrayList remove(object) method. We're creating a ArrayList of String. We're adding couple of Strings to the ArrayList object using add() method calls per element. Array size is printed, array is printed and using remove(object) method, an element is removed. Then size and array is printed again.

package com.tutorialspoint;
import java.util.ArrayList;
public class ArrayListDemo {
   public static void main(String[] args) {
      
      // create an empty array list
      ArrayList<String> arrayList = new ArrayList<>();

      // use add() method to add elements in the arrayList
      arrayList.add("A");
      arrayList.add("B");
      arrayList.add("C");
      arrayList.add("D");

      System.out.println("Size of list: " + arrayList.size());
	   // let us print all the elements available in list again
      System.out.println("ArrayList = " + arrayList);
      
      // Removes element B
      arrayList.remove("B");

      System.out.println("Now, Size of list: " + arrayList.size());
      
      // let us print all the elements available in list again
      System.out.println("ArrayList = " + arrayList);
   }
}   

Output

Let us compile and run the above program, this will produce the following result −

Size of list: 4
ArrayList = [A, B, C, D]
Now, Size of list: 3
ArrayList = [A, C, D]

Example 3

The following example shows the usage of Java ArrayList remove(index) method. We're creating a ArrayList of Student objects. We're adding couple of Students to the ArrayList object using add() method calls per element. Array size is printed, array is printed and using remove(index) method, an element is removed. Then size and array is printed again.

package com.tutorialspoint;
import java.util.ArrayList;
public class ArrayListDemo {
   public static void main(String[] args) {
      
      // create an empty array list
      ArrayList<Student> arrayList = new ArrayList<>();

      // use add() method to add elements in the arrayList
      arrayList.add(new Student(1, "Julie"));
      arrayList.add(new Student(2, "Robert"));
      arrayList.add(new Student(3, "Adam"));
      System.out.println("Size of list: " + arrayList.size());
	   
      // let us print all the elements available in list again
      System.out.println("ArrayList = " + arrayList);
      
      // Removes element at 3rd position
      arrayList.remove(2);
      System.out.println("Now, Size of list: " + arrayList.size());
      
      // let us print all the elements available in list again
      System.out.println("ArrayList = " + arrayList);
   }
}
class Student {
   int rollNo;
   String name;
   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }
   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}   

Output

Let us compile and run the above program, this will produce the following result −

Size of list: 3
ArrayList = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
Now, Size of list: 2
ArrayList = [[ 1, Julie ], [ 2, Robert ]]
java_util_arraylist.htm
Advertisements