Add, retrieve and remove element from ArrayList

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


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.

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
                System.out.println("Elements of first ArrayList: "+al);

                ArrayList<String> al2 = new ArrayList<String>();
                //Adding the both array
                System.out.println("Elements of second ArrayList: "+al2);
                //remove the element
                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));


array list