public class Vector implements List
The Vector class is similar to an ArrayList as it also implements dynamic array. Vector class stores an array of objects and the size of the array can increase or decrease. The elements in the Vector can be accessed using an integer index.
The difference between Vector and ArrayList class is that the methods of Vector are synchronised are thred-safe.
Constructor
|
Vector() Constructs an empty vector so that its internal data array has size 10 and its standard capacity increment is zero.
|
Vector(int initialCapacity) Constructs an empty vector with the specified initial capacity and with its capacity increment equal to zero.
|
Vector(int initialCapacity, int capacityIncrement) Constructs an empty vector with the specified initial capacity and capacity increment.
|
Some Important Methods
| |
void
|
addElement(E obj) Adds the specified component to the end of this vector,
increasing its size by one.
|
Int
|
capacity() Returns the current capacity of this vector
|
void
|
clear() Removes all of the elements from this Vector.
|
E
|
elementAt(int index) Returns the component at the specified index.
|
boolean
|
removeElement(Object obj) Removes the first (lowest-indexed) occurrence of the argument from this vector.
|
void
|
removeElementAt(int index) Deletes the component at the specified index
|
Object[]
|
toArray() Returns an array containing all of the elements in this Vector in
the correct order.
|
Example:
import java.util.Vector;
public class VectorDemo
{
{
public static void main(String args[])
{
{
Vector v = new Vector();
v.addElement("Harry");
v.addElement("Emaini");
v.addElement("Voldmort");
v.addElement("snape");
v.insertElementAt("Dumbuldore",0);
v.insertElementAt("Tom",4);
System.out.println("Contents of Vector :");
int count = 0;
while(count < v.size())
{
{
System.out.print(v.elementAt(count));
count++;
if(count < v.size())
System.out.print(", ");
}
System.out.println("\nSize : "+ v.size());
v.removeElement("Emaini");
System.out.println("\nContents of Vector after removing Emaini :");
count = 0;
while(count < v.size())
{
{
System.out.print(v.elementAt(count));
count++;
if(count < v.size())
System.out.print(", ");
}
System.out.println("\nSize : " + v.size());
System.out.println("\nFirst Element = " + v.firstElement());
System.out.println("Default Capacity = " + v.capacity());
System.out.println("Last Element = " + v.lastElement());
}
}
0 comments :
Post a Comment