Arraylist in java

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

— Serialize
— Clonable
— 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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

Your email address will not be published. Required fields are marked *