ArrayList Vector
ArrayList Vector
Description:
ArrayList is a part of collection framework and is present in java.util package. It provides us dynamic arrays
in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of
manipulation in the array is needed.
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Iterator;
Vector in Java
Java.util.Vector Class in Java. The Vector class implements a growable array of objects. Vectors basically
falls in legacy classes but now it is fully compatible with collections. They are very similar to ArrayList but
Vector is synchronised and have some legacy method which collection framework does not contain.
The Vector class implements a growable array of objects. Vectors basically falls in legacy classes but now it
is fully compatible with collections.
• Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it
contains components that can be accessed using an integer index
• They are very similar to ArrayList but Vector is synchronised and have some legacy method which
collection framework does not contain.
• It extends AbstractList and implements List interfaces.
import java.util.*;
public class VectorDemo {
v.addElement(new Integer(1));
v.addElement(new Integer(2));
v.addElement(new Integer(3));
v.addElement(new Integer(4));
System.out.println("Capacity after four additions: " + v.capacity());
v.addElement(new Double(5.45));
System.out.println("Current capacity: " + v.capacity());
v.addElement(new Double(6.08));
v.addElement(new Integer(7));
System.out.println("Current capacity: " + v.capacity());
v.addElement(new Float(9.4));
v.addElement(new Integer(10));
System.out.println("Current capacity: " + v.capacity());
v.addElement(new Integer(11));
v.addElement(new Integer(12));
System.out.println("First element: " + (Integer)v.firstElement());
System.out.println("Last element: " + (Integer)v.lastElement());
if(v.contains(new Integer(3)))
System.out.println("Vector contains 3.");
while(vEnum.hasMoreElements())
System.out.print(vEnum.nextElement() + " ");
System.out.println();
}
}