Monday 2 January 2012

ArrayList

what is arraylist in java with suitable example
ArrayList 

                An ArrayList is like an array ,which can grow in memory dynamically.it means that we can store elements into the ArrayList,depending on the number of elements,the memory is dynamically allotted and re-allotted accommodate all the elements.what is Arraylist in java with suitable example, what is arraylist in java with  program,java arraylist program easy
  • Duplicate objects are allowed
  •   Insertion order preserved
  • Heterogeneous objects are allowed 
  • Null insertion is possible
  •  ArrayList is not synchronized .this means that when more than one thread acts simultaneously on the ArrayList object,the results may be incorrect in some cases.what is arraylist in java with suitable example  

ArrayList constructor

   ArrayList al=new ArrayList();   creates an empty ArrayList object with default initial capacity 10.

if ArrayList reaches max capacity then a new ArrayList object will be  with

                        new   capacity=(current capacity*3/2)+1

ArrayList al=new ArrayList(int initialcapacity);creates an empty ArrayList object with initial capacity

ArrayList al=new ArrayList(Collection c);   creates an equivalent ArrayList object with the given collection.

ArrayList Example
what is arraylist in java with suitable example
import java.util.ArrayList;

Class ArrayListDemo{

                  public static void main(String args[]) {   


      ArrayList al = new ArrayList();// constructs a new empty ArrayList  

                          al.add("a");   //to add an  element

                         al.add("10");

                         al.add("A");

                         al.add("a");

                         al.add(null);     //inserting null value

                         System.out.println("ArrayList object ");

                          System.out.println(al);   

                           al.remove(2);     //removing specific element

                           System.out.println("ArrayList after remove an element");

                           System.out.println(al);

                            al.add(2,"a");

                            System.out.println("ArrayList after adding"); 

                            System.out.println(al); 



}

}
output:Arraylist object
 [a,10,A,a,null]
ArrayList after removing
[a,10,a,null]
ArrayList after adding 
[a,10,a,a,null] 










No comments:

Post a Comment