wellnwill Marathahalli Bangalore

Vector

It is legacy class, introduced from java 1.0.

Methods are synchronized.

Multiple threads cannot access one method simultaneously i.e. thread safe.

Performance is low as compare to ArrayList.

We can access data through Enumeration, Iterator and ListIterator cursors.

Enumeration:

The purpose of the cursor is to fetch the data from the collection objects. Enumeration cursor is only used with Vector and Stack classes.

By using this cursor, we can read the data. We cannot update or delete elements through Enumaration.

How to create Enumeration object:

by using elements() method of Vector class.

public Enumeration elements();

Methods for Enumeration object:

  • public Boolean hasMoreElements();
  • public Object nextElement();

Constructor

Vector v=new Vector(); creates an empty Vector object with default size 10.

Once Vector default size is full then a new Vector object will be created with new size (i.e. current size*2).

Vector v=new Vector(int initail capacity); creates an empty vector object with specified initial capacity.

Vector v=new Vector(int initail capacity, int incremental capacity);

Vector v=new Vector(1000,5)

Once Vector object reaches 1000 capacity then a new Vector object will be created with capacity 1005.

Vector v=new Vector(Collection c); creates an equivalent vector object for the given collection i.e. interconversion between collection objects.

Commonly used methods:

To add the objects:

public boolean add(Object ob): method of Collection interface.

public void add(int index,Object element): method of List interface.

public void addElement(Object ob): method of Vector class.

public Object removeFirst():

This method removes and returns the first element from LinkedList.

To remove object:

public void removeElement(Object ob): This method of Vector class is used to remove the vector element.

public void removeElementAt(int index): ): This method of Vector class is used to remove the vector element of specified index.

public void removeAllElement():This method of Vector class is used to remove the vector element.

public void clear():method of Collection interface.

public void remove(Object ob):method of Collection interface.

public void remove(int index): method of List interface.

To get objects at specific index:

Object elementAt(int index) : This method of Vector class will return the element at specified index.

Object firstElement(): This method of Vector class returns the first element of Vector object.

Object lastElement(): This method of Vector class returns the last element of Vector object.

Object get(int index): method of List interface.

int size(): returns the size of Vector object.

int capacity(): returns the capacity of Vector object.

                                                      
import java.util.*;
class Test242
{
public static void main(String[] args)
{
Vector v=new Vector();
System.out.println("size:"+v.size());
v.addElement("A");
v.addElement("B");
v.addElement("C");
v.addElement("D");
v.addElement("E");
System.out.println(v);
System.out.println("size:"+v.size());
System.out.println("capacity:"+v.capacity());
Enumeration e1=v.elements();
while (e1.hasMoreElements())
{
String s=(String)e1.nextElement();
System.out.println(s);
}
for(Object ob:v)
{
System.out.println(ob);	
}
}
}
import java.util.*;
class Test243
{
public static void main(String[] args)
{
Vector v=new Vector();
v.addElement("A");
v.addElement("B");
v.addElement("C");
v.addElement("D");
v.addElement("E");
System.out.println(v);
v.removeElement("A");
System.out.println(v);
Enumeration e1=v.elements();
while (e1.hasMoreElements())
{
String s=(String)e1.nextElement();
System.out.println(s);
}
v.clear();
System.out.println(v);
}
}




Footer with Map

Top