ArrayList


1) The underlying data structure is a resizable array (or) growable array.

2) Duplicate objects are allowed.

3) Insertion order preserved.

4) Heterogeneous objects are allowed.(except TreeSet , TreeMap every where heterogenious objects are allowed).

5) Null insertion is possible.

Constructors:

1) ArrayList a=new ArrayList();
Creates an empty ArrayList object with default initial capacity "10" if ArrayList reaches its max capacity then a new ArrayList object will be created with New capacity=(current capacity*3/2)+1 2) ArrayList a=new ArrayList(int initialcapacity); Creates an empty ArrayList object with the specified initial capacity. 3) ArrayList a=new ArrayList(collection c); Creates an equivalent ArrayList object for the given Collection that is this constructor meant for inter conversation between collection objects. That is to dance between collection objects.

Demo program for ArrayList:

import java.util.*;
class ArrayListDemo { public static void main(String[] args) { ArrayList a=new ArrayList(); a.add("A"); a.add(10); a.add("A"); a.add(null); System.out.println(a);//[A, 10, A, null] a.remove(2); System.out.println(a);//[A, 10, null] a.add(2,"m"); a.add("n"); System.out.println(a);//[A, 10, m, null, n] } }

Usually, we can use collection to hold and transfer objects from one tier to another tier. To provide support for this requirement every Collection class already implements Serializable and Cloneable interfaces.

ArrayList and Vector classes implements RandomAccess interface so that any random element we can access with the same speed. Hence ArrayList is the best choice of "retrival operation".

RandomAccess interface present in util package and doesn't contain any methods. It is a marker interface.

Example :
ArrayList a1=new ArrayList();
LinkedList a2=new LinkedList(); System.out.println(a1 instanceof Serializable ); //true System.out.println(a2 instanceof Clonable); //true System.out.println(a1 instanceof RandomAccess); //true System.out.println(a2 instanceof RandomAccess); //false

Vector

1) The underlying data structure is resizable array (or) growable array.

2) Duplicate objects are allowed.

3) The insertion order is preserved.

4) Heterogeneous objects are allowed.

5) Null insertion is possible.

Implements Serializable, Cloneable and RandomAccess interfaces.

Every method present in Vector is synchronized and hence Vector is Thread safe.

Vector specific methods: To add objects:

add(Object o);-----Collection
add(int index,Object o);-----List addElement(Object o);-----Vector

To remove elements:

remove(Object o);--------Collection
remove(int index);--------------List removeElement(Object o);----Vector removeElementAt(int index);-----Vector removeAllElements();-----Vector clear();-------Collection To get objects: Object get(int index);---------------List Object elementAt(int index);-----Vector Object firstElement();--------------Vector Object lastElement();---------------Vector

Other methods:

Int size();//How many objects are added
Int capacity();//Total capacity Enumeration elements();
Constructors:
Vector v=new Vector();

Creates an empty Vector object with default initial capacity 10.
Once Vector reaches its maximum capacity then a new Vector object will be created with double capacity. 
That is "newcapacity=currentcapacity*2".
Vector v=new Vector(int initialcapacity);
Vector v=new Vector(int initialcapacity, int incrementalcapacity); Vector v=new Vector(Collection c);
Example:
import java.util.*;
class VectorDemo { public static void main(String[] args) { Vector v=new Vector(); System.out.println(v.capacity());//10 for(int i=1;i< =10;i++) v.addElement(i); System.out.println(v.capacity());//10 v.addElement("A"); System.out.println(v.capacity());//20 System.out.println(v);//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, A] } }

Differences between ArrayList and Vector

  ArrayList Vector
 1 No method is synchronized. Every method is synchronized.
 2At a time multiple Threads are allow to operate on ArrayList object and hence ArrayList object is not Thread safe. At a time only one Thread is allow to operate on Vector object and hence Vector object is Thread safe.
 Relatively performance is high because Threads are not required to wait. Relatively performance is low because Threads are required to wait.
 4 It is non-legacy and introduced in 1.2v. It is a legacy and introduced in 1.0v.