Vector Class in Java

Introduction

 
The Vector class implements a growable array (dynamic array). In the vector class, a component is stored at an integer index and it can be accessed by this index number like an Array. A Vector is synchronized and it contains many legacy methods that are not part of the collection framework. As of Java1.2, the Vector class has been retrofitted to implement a List.
 

Hierarchy of vector class

 
java-collection.jpg 
 

Constructor Summary 

  • Vector():  Creates an empty vector so that its internal data array has size 10 and its standard capacity increment is zero.

  • Vector(Collection<? extends E> c):  Creates a vector containing the elements of the specified collection, in the order, they are returned by the collection's iterator.

  • Vector(int initialCapacity):  Creates an empty vector with the specified initial capacity and with its capacity increment equal to zero.

  • Vector(int initialCapacity, int capacityIncrement):  Creates an empty vector with the specified initial capacity and capacity increment. 

Method Summary

 
Methods for Add elements to this vector: The following methods are used to add an element of the vector class.
 
Return type Method name Description of functionality
boolean add(E e) Appends the specified element to the end of this Vector.
void add(int index, E element) Inserts the specified element at the specified position in this Vector.
boolean addAll(Collection<? extends E> c)  Appends all of the elements in the specified Collection to the end of this Vector, in the order that they are returned by the specified Collection's Iterator.
boolean addAll(int index, Collection<? extends E> c) Inserts all of the elements in the specified Collection into this Vector at the specified position.
void addElement(E obj) Adds the specified component to the end of this vector, increasing its size by one.
void insertElementAt(E obj, int index) Inserts the specified object as a component in this vector at the specified index.
 
Methods for Size and capacity: The following methods are used to determine the capacity and size of the vector.
 
Return type Method name Description of functionality
int capacity() Returns the current capacity of this vector.
void ensureCapacity(int minCapacity) Increases the capacity of this vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument.
void setSize(int newSize) Sets the size of this vector.
int size() Returns the number of components in this vector.
void trimToSize() Trims the capacity of this vector to be the vector's current size.
 
Methods for Remove all elements from a vector: Used for removing all the elements of the vector.
 
Return type Method name Description of functionality
void clear() Removes all of the elements from this Vector.
 
Clone a vector: This method is used for making an object clone of the vector class.
 
Return type Method name Description of functionality
Object clone() Returns a clone of this vector
 
Methods for check contain a certain element: These methods are for checking whether elements are present or not.
 
Return type Method name Description of functionality
boolean contains(Object o) Returns true if this vector contains the specified element.
boolean containsAll(Collection<?> c) Returns true if this Vector contains all of the elements in the specified Collection.
 
Methods for Copy elements: This method is used to copy elements of a vector into an array.
 
Return type Method name Description of functionality
void copyInto(Object[] anArray) Copies the components of this vector into the specified array.
 
Methods for Get Enumeration from this vector: This method is for getting an enumeration object from a vector class.
 
Return type Method name Description of functionality
Enumeration<E> elements() Returns an enumeration of the components of this vector.
 
Methods for Compare two vectors: This method is used to compare the elements of two vector class objects.
 
Return type Method name Description of functionality
boolean equals(Object o) Compares the specified Object with this Vector for equality.
 
Methods for Get element from vector: Using are following methods we can get the elements of a vector object as needed.
 
Return type Method name Description of functionality
E elementAt(int index) Returns the component at the specified index
E firstElement() Returns the first component (the item at index 0) of this vector.
E get(int index) Returns the element at the specified position in this Vector
E lastElement() Returns the last component of the vector.
 
Methods for check is vector empty: This method is used to check whether a vector is empty or not.
 
Return type Method name Description of functionality
boolean isEmpty() Tests if this vector has no components.
 
Methods for Get element index: These methods are useful for finding the indexing position of an element.
 
Return type Method name Description of functionality
int indexOf(Object o) Returns the index of the first occurrence of the specified element in this vector, or -1 if this vector does not contain the element.
int indexOf(Object o, int index) Returns the index of the first occurrence of the specified element in this vector, searching forwards from the index, or returns -1 if the element is not found.
int lastIndexOf(Object o) Returns the index of the last occurrence of the specified element in this vector, or -1 if this vector does not contain the element.
int lastIndexOf(Object o, int index) Returns the index of the last occurrence of the specified element in this vector, searching backward from the index, or returns -1 if the element is not found.
 
Methods for Remove element from a vector.
 
Return type Method name Description of functionality
E remove(int index) Removes the element at the specified position in this Vector.
boolean remove(Object o) Removes the first occurrence of the specified element in this Vector If the Vector does not contain the element, it is unchanged.
boolean removeAll(Collection<?> c) Removes from this Vector all of its elements that are contained in the specified Collection.
void removeAllElements() Removes all components from this vector and sets its size to zero.
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
 
Methods for Retain elements collection c has.
 
Return type Method name Description of functionality
boolean retainAll(Collection<?> c) Retains only the elements in this Vector that are contained in the specified Collection.
 
Methods for Replace element in a vector.
 
Return type Method name Description of functionality
E set(int index, E element) Replaces the element at the specified position in this Vector with the specified element.
void setElementAt(E obj, int index) Sets the component at the specified index of this vector to be the specified object.
 
Methods for Get a sub list from a list.
 
Return type Method name Description of functionality
List<E> subList(int fromIndex, int toIndex) Returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive.
 
16-Converts Vector to Array.
 
Return type Method name Description of functionality
Object[] toArray() Returns an array containing all of the elements in this Vector in the correct order.
<T> T[] toArray(T[] a) Returns an array containing all of the elements in this Vector in the correct order; the runtime type of the returned array is that of the specified array.
String toString() Returns a string representation of this Vector, containing the String representation of each element.
 
Example
 
First look at the code of this program carefully and read the used method detail, which is given in an appropriate table and after this, you can understand the following output very easily.
  1. import java.util.*;  
  2. class VectorClassDemo {  
  3.     public static void main(String args[]) {  
  4.         // here we call the 4th type constructor in constructor list  
  5.         Vector v = new Vector(53);  
  6.         //for findout the intial size use the size() method   
  7.         System.out.println("Initial size: " + v.size());  
  8.         //for findout the intial capacity we use the capacity() method   
  9.         System.out.println("Initial capacity: " +  
  10.             v.capacity());  
  11.         // for add element in vector we use addElement() method  
  12.         v.addElement(new Integer(10));  
  13.         v.addElement(new Integer(11));  
  14.         v.addElement(new Integer(12));  
  15.         v.addElement(new Integer(13));  
  16.         v.addElement(new Integer(14));  
  17.         System.out.println("After inserting five element  the capacity is : " +  
  18.             v.capacity());  
  19.         // you insert one more element it exceed the current capacity so its increased by 3 accordind to constructor  
  20.         v.addElement(new Double(17.045));  
  21.         System.out.println("now capacity: " +  
  22.             v.capacity());  
  23.         v.addElement(new Double(3.08));  
  24.         v.addElement(new Integer(12));  
  25.         System.out.println("now capacity: " +  
  26.             v.capacity());  
  27.         v.addElement(new Float(7.4));  
  28.         v.addElement(new Integer(10));  
  29.         System.out.println("after inserting 3 more element so now capacity: " +  
  30.             v.capacity());  
  31.         v.addElement(new Integer(18));  
  32.         v.addElement(new Integer(19));  
  33.         // for find the first element firstElement() method is used  
  34.         System.out.println("First element: " + (Integer) v.firstElement());  
  35.         // for find the last element lastElement() method is used  
  36.         System.out.println("Last element: " + (Integer) v.lastElement());  
  37.         // contains method check this elements is present or not  
  38.         if (v.contains(new Integer(3)))  
  39.             System.out.println("Vector contains 3.");  
  40.         //here elements() method return enumerate object  
  41.         Enumeration venumeration = v.elements();  
  42.         System.out.println("\nElements in vector:");  
  43.         while (venumeration.hasMoreElements())  
  44.             System.out.print(venumeration.nextElement() + "-->");  
  45.         System.out.println();  
  46.     }  
  47. }  
OUTPUT
 
vector cmd.gif
 


Similar Articles