Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to use ArrayList in Java with example ?.


ArrayList is a simple Collection which is provided by the Java Collection Framework API. The class ArrayList is dynamic implementation of an array. ArrayList is like an array which can grow dynamically.


Program to demonstrate how to implement ArrayList class in java.util.*; package along with its operations :

package com.hubberspot.collections.example;

import java.util.*;

public class ArrayListExample 
{
   public static void main(String args[]) 
   {
      ArrayList list = new ArrayList();
      
      System.out.println("Initial size of list :" +
        " " + list.size());
     
      list.add("A");
      list.add("B");
      list.add("C");
      list.add("D");
      list.add("E"); 
      list.add("F");
      list.add(1, "1");

      System.out.println("Size after addition :" +
        " " +list.size());

      System.out.println("Contents of list: " + list);
      
      for(Object str : list)
        System.out.println(str);
      
      list.remove("F");
      list.remove(2);

      System.out.println("Size of after deletion : "
                + list.size());
      System.out.println("Contents of list : " + list);
 } 
}


Output of the program :


 
© 2021 Learn Java by Examples Template by Hubberspot