Arraylist in java

Arraylist

  • Arraylist is a implementation class of list interface.
  •  Arraylist implemenet marker interface

— Serialize
— Cloneable
— Random access

  • Arraylist built on re-sizable array data structure.
  • Arralist size grows with a below formula.

new capacity=(old capacity 3/2)+1

  • Array list class has 3 overload constructor.
    1. no arg constructor
    2. int arg constructor
    3. collection type arg constructor.
  • In Arraylist accessing and retrieval of element is faster compared to linkedlist.
When to use Arraylist ?

  •  Arraylist preserve insertion order.
  • In arraylist each element has index so searching is easy if index is known.
  •  Duplicate object insertion is allowed
  • Null insertion is allowed.

so if you want above functionality then array list is preferable.

Example of Arraylist 
package collectionConcept;

import java.util.*;

 class Arraylist2
 { 
	 
   public static void main(String args[])
   {  
   
     ArrayList al=new ArrayList();//creating arraylist  
     al.add("Ravi");//adding object in arraylist  
     al.add("Vijay");  
     al.add("Ravi");  
     al.add("Ajay");  
  
   Iterator itr=al.iterator();
   //getting Iterator from arraylist to traverse elements  

   while(itr.hasNext())
   {  
     System.out.println(itr.next());  
   }  
  
  }  
}  

output:

Ravi
Vijay
Ravi
Ajay

Different ways to retrieve Arraylist element
1. Using for loop
2. Using for each loop
3. Using Iterator
4. Using Listiterator

Leave a Reply