ArrayList remove(int index) method in java

Let’s learn ArrayList remove(int index) method in java.

ArrayList remove(int index) method in java

remove(int index) method ArrayList class removes the element at the specified position in this list.

Shifts any subsequent elements to the left (subtracts one from their indices).

Syntax:

public remove(int index)

Parameters:

index the index of the element to be removed.

Returns:

the element that was removed from the list.

Also read – ArrayList in java

Now let’s see example on ArrayList remove(int index) method.

import java.util.ArrayList;
public class ArrayListRemoveIndexMethodExample
{
   public static void main(String[] args)
   {
      ArrayList<Integer> al = new ArrayList<Integer>(5);
      al.add(56);
      al.add(29);
      al.add(76);
      al.add(94);
      System.out.println("ArrayList size: " + al.size());
      for(Integer number : al)
      {
         System.out.println("Number: " + number);
      }
      // removing element at third position
      al.remove(2);
      System.out.println("ArrayList size after using remove(int index) method: " + al.size());
      // printing all elements in list
      for(Integer number : al)
      {
         System.out.println("Number: " + number);
      }
   }
}


Output:

ArrayList size: 4
Number: 56
Number: 29
Number: 76
Number: 94
ArrayList size after using remove(int index) method: 3
Number: 56
Number: 29
Number: 94