
Vector Class in Java
Vector Class in Java
- Vector class extends AbstractList and implements List, RandomAccess, Cloneable, Serializable.
- It is based on reserved memory storage called capacity.
- It will maintain default capacity of 10 bytes. It allows user can define their own capacity based on constructor.
- It allows to retrieve first and last element respectively.
- It also a synchronized class like StringBuffer class.
Creating Vector:
Vector obj=new Vector();\\with default capacity of 10
Vector obj=new Vector(int);\\with userdefined capacity.
Vector obj=new Vector(int,int);\\With userdefined and incremental
Methods:
int capacity()
It returns the capacity reserved by the vector.
void ensureCapacity(int)
int – no.of bytes to store
It returns the more no.of bytes to reserve
void setSize(int)
int – size of the vector
Used to set the size for the vector.
E elementAt(int)
int – index of the element
It returns the specified indexed element like get(int)
E firstElement()
It returns the first element in the vector.
E lastElement()
It returns the last element in the vector.
void setElement(E,int)
E – Element , int –index to store
Used to set the Element in the specified index
void addElement(int)
add the element at last
void removeElementAt(int)
int – index to remove
It removes the Element in vector at specified index
void insertElementAt(E,int)
E – Element , int – index to insert
Used to insert element in the specified index
Enumeration elements()
It returns the Elements to Enumeration interface
Example:
import java.util.*;
public class ListVector
{ public static void main(String args[])
{
Vector v=new VectoPreview (opens in a new window)r(2,3);
System.out.println("Capacity(before) :"+v.capacity());
System.out.println("Size of Vector(before):"+v.size());
v.add(2);
v.add(3);
v.add("asd");
v.add('d');
v.add("asd");
v.add('d');
System.out.println("Capacity (After):"+v.capacity());
System.out.println("Size of Vector(After):"+v.size());
System.out.println("Elements(Clone):"+v.clone());
System.out.println("First Elements:"+v.firstElement());
System.out.println("Last Elements:"+v.lastElement());
Enumeration e=v.elements();
while(e.hasMoreElements())
{
System.out.println("Elements :"+e.nextElement());
}
Iterator i=v.iterator();
while(i.hasNext())
{
System.out.println("Elements:"+i.next());
}
v.setElementAt("hello", 4);
System.out.println("Elements After setElementAt:"+v.clone());
v.removeElementAt(2);
System.out.println("Elements After removeElementAt:"+v.clone());
v.addElement("world");
System.out.println("Elements After addElement:"+v.clone());
System.out.println("Element at:"+v.elementAt(3));
}
}
|
Output:
Capacity(before) :2
Size of Vector(before):0 Capacity (After):8 Size of Vector(After):6 Elements(Clone):[2, 3, asd, d, asd, d] First Elements:2 Last Elements:d Elements :2 Elements :3 Elements :asd Elements :d Elements :asd Elements :d Elements:2 Elements:3 Elements:asd Elements:d Elements:asd Elements:d Elements After setElementAt:[2, 3, asd, d, hello, d] Elements After removeElementAt:[2, 3, d, hello, d] Elements After addElement:[2, 3, d, hello, d, world] |