Pin It

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 :


Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot