Add, retrieve and remove element from ArrayList

Q. Write a program to add, retrieve and remove the element from the ArrayList.

Answer:

An ArrayList class is a dynamic data structure, which can add and remove the data dynamically. It allows random access of the elements.
Below is the  example of ArrayList. It performs addition, retrieval and deletion of elements from the ArrayList. We create generic list which accepts only String type value.

ArrayListDemo.java

import java.util.*;
class ArrayListDemo
{
        public static void main(String[] args)
        {
                ArrayList<String> al = new ArrayList<String>();
                System.out.println("Size of ArrayList: "+al.size());
                //Adding the elements
                al.add("Java");
                al.add("JDBC");
                System.out.println("Elements of first ArrayList: "+al);

                ArrayList<String> al2 = new ArrayList<String>();
                al2.add("EJB");
                al2.add("Struts");
                //Adding the both array
                al2.addAll(al);
                System.out.println("Elements of second ArrayList: "+al2);
                //remove the element
                al2.remove("EJB");
                System.out.println("Elements of ArrayList after deletion: "+al2);
                System.out.println("Size of ArrayList: "+al2.size());

                //Retriving 2nd index element
                System.out.println("The element at 2nd index is: "+al2.get(2));
        }
}


Output:

array list