Remove Element from ArrayList Example Java Program


Definition

An ArrayList is a non-syncronized class. It implements List Interface and inherits AbstractList class. It maintains insertion order. 

Syntax

arraylist.remove(index);

Remove Element Example Program

import java.util.ArrayList;

public class RemoveElementExample {

    public static void main(String[] args) {
        ArrayList<String> animalsArrayList = new ArrayList<String>();

        animalsArrayList.add("Lion");
        animalsArrayList.add("Tiger");
        animalsArrayList.add("Camel");
        animalsArrayList.add("Giraffee");
        animalsArrayList.add("Deer");
        animalsArrayList.add("Bear");
        animalsArrayList.add("Cheetah");
        animalsArrayList.add("Hyena");

        System.out.println("Initial ArrayList is : " + animalsArrayList);//Printing initial arraylist
        animalsArrayList.remove(3); //Removing element at position 3
        System.out.println("ArrayList after removal is : " + animalsArrayList);//Printing arraylist after removing element at position 3 - Giraffee

    }
}

Sample Output

Initial ArrayList is : [Lion, Tiger, Camel, Giraffee, Deer, Bear, Cheetah, Hyena]
ArrayList after removal is : [Lion, Tiger, Camel, Deer, Bear, Cheetah, Hyena]