wellnwill Marathahalli Bangalore

ArrayList

Subclasses of List interface

ArrayList class

ArrayList class object represent the functionality of dynamic or resizable array. ArrayList is fast for searching operation because it implements a RandomAccess interface.

ArrayList is slow to insert and delete operation because internally several shift operation are performed.

Commonly used constructors

public ArrayList():

creates an empty ArrayList object with default initial capacity 10.

Once ArrayList reaches its max capacity then a new ArrayList object will be created with new capacity = (current capacity*3/2)+ 1.

public ArrayList(Collection c):

creates an equivalent ArrayList object for the given collection. The purpose of this contractor is interconversion between collection objects.

public ArrayList(int intialCapacity):

creates an empty ArrayList object with specified initial capacity. Resizing of an array:

RandomAccess interface:

It is a marker interface and present in java.util package.

Marker interface:

Marker interface does not have any members. This type of interface is required to provide some essential information to JVM.

                                                      
import java.util.*;
class Test232{
public static void main(String args[]){
Collection c=new ArrayList();
//adding elements into Collection
c.add("Pune");
c.add("Lucknow");
c.add("Mysore");
c.add("Bangalore");
System.out.println(c);//[Pune,Lucknow,Mysore,bangalore]
Iterator itr=c.iterator();
while(itr.hasNext())
{
 Object obj=itr.next();
 System.out.println(obj);
 if(obj.equals("Mysore"))
 {
  itr.remove();
 }
}
System.out.println(c); //[Pune,Lucknow, bangalore]
}
}
                                                       
public class Employee{  
  int id;  
  String name;  
  int salary;  
 public Employee(int id,String name,int salary){  
   this.id=id;  
   this.name=name;  
   this.salary=salary;  
  }  
}
public class Programmer{  
  int id;  
  String name;  
  int salary;  
 public Programmer(int id,String name,int salary){  
   this.id=id;  
   this.name=name;  
   this.salary=salary;  
  }  
}

                                                       
import java.util.*;  
public class Test236{  
 public static void main(String args[]){  
  Employee e1=new Employee(501,"Raj",4000);  
  Employee e2=new Employee(502,"David",5000);  
  ArrayList al=new ArrayList(); 
  al.add(e1);  
  al.add(e2);  
    
  Iterator itr=al.iterator();   
  while(itr.hasNext()){  
    Employee e=(Employee)itr.next();  
    System.out.println(e.id+" "+e.name+" "+e.salary);  
  }
 }  
}  

public class Programmer{  
  int id;  
  String name;  
  int salary;  
 public Programmer(int id,String name,int salary){  
   this.id=id;  
   this.name=name;  
   this.salary=salary;  
  }  
}








                                                      
import java.util.*;  
public class Test237{  
 public static void main(String args[]){  
  Employee e1=new Employee(501,"Raj",4000);  
  Employee e2=new Employee(502,"David",5000); 
  Programmer p1=new Programmer(503,"John",4500);  
  ArrayList al=new ArrayList(); 
  al.add(e1);  
  al.add(e2);
  al.add(p1);  
  Iterator itr=al.iterator();   
  while(itr.hasNext()){  
    Employee e=(Employee)itr.next();  
    System.out.println(e.id+" "+e.name+" "+e.salary);  
  }  
 }
} // Note-Runtime Exception –ClassCastException.
	
                                                       
import java.util.*;  
public class Test238
{  
 public static void main(String args[]){  
  Employee e1=new Employee(501,"Raj",4000);  
  Employee e2=new Employee(502,"David",5000); 
  Programmer p1=new Programmer(503,"John",4500);  
  ArrayList al=new ArrayList(); 
  al.add(e1);  
  al.add(e2);
  al.add(p1);  
  Iterator itr=al.iterator();   
  while(itr.hasNext()){ 
    Object obj=itr.next();
	if(obj instanceof Employee)
	{
    Employee e=(Employee)obj;
	System.out.println(e.id+" "+e.name+" "+e.salary); 
    }  
	}  
 }  
 } 
// O/P-501 Raj 4000
       502 David 5000

import java.util.*;  
public class Test239
{  
 public static void main(String args[])
 {  
  Employee e1=new Employee(501,"Raj",4000);  
  Employee e2=new Employee(502,"David",5000); 
  Programmer p1=new Programmer(503,"John",4500);  
  ArrayList al=new ArrayList(); 
  al.add(e1);  
  al.add(e2);
  //al.add(p1);  cannot add object other then Employee
   Iterator itr=al.iterator();   
   while(itr.hasNext()){  
   Employee e=(Employee)itr.next();  
   System.out.println(e.id+" "+e.name+" "+e.salary);  
  }  
 }  
}






   
Footer with Map