Vector in java

Vector implements a dynamic array. It is similar to ArrayList, but with two differences: Vector is synchronized, and it contains many legacy methods that are not part of the Collections Framework. With the advent of collections, Vector was reengineered to extend AbstractList and to implement the List interface. With the release of JDK 5, it was retrofitted for generics and reengineered to implement Iterable. This means that Vector is fully compatible with collections, and a Vector can have its contents iterated by the enhanced for loop.

Vector is declared like this:

class Vector<E>

Here, specifies the type of element that will be stored.

Here are the Vector constructors:

Vector( )

Vector(int size)

Vector(int size, int incr)

Vector(Collection<? extends E> c)

The first form creates a default vector, which has an initial size of 10. The second form creates a vector whose initial capacity is specified by size. The third form creates a vector whose initial capacity is specified by size and whose increment is specified by incr. The increment specifies the number of elements to allocate each time that a vector is resized upward. The fourth form creates a vector that contains the elements of collection c

Vector defines these protected data members:

int capacityIncrement; 
int elementCount;
 Object[ ] elementData;
Method Description 
void addElement(E element)The object specified by element is added to the vector. 
    
int capacity( ) Returns the capacity of the vector. 
    
Object clone( ) Returns a duplicate of the invoking vector. 
   
boolean contains(Object element)Returns true if element is contained by the vector, and returns false if it is not. 
   
void copyInto(Object array[ ])The elements contained in the invoking vector are copied into the array 
  specified by array. 
   
E elementAt(int index)Returns the element at the location specified by index. 
   
Enumeration<E> elements( )Returns an enumeration of the elements in the vector. 
   
void ensureCapacity(int size)Sets the minimum capacity of the vector to size. 
    
E firstElement( ) Returns the first element in the vector. 
   
int indexOf(Object element)Returns the index of the first occurrence of element. If the object is not in the 
  vector, –1 is returned. 
   
int indexOf(Object element, int start)Returns the index of the first occurrence of element at or after start. If the object 
  is not in that portion of the vector, –1 is returned. 
   
void insertElementAt(E element,Adds element to the vector at the location specified by index. 
 int index)  
   
boolean isEmpty( )Returns true if the vector is empty, and returns false if it contains one or more 
  elements. 
    
E lastElement( ) Returns the last element in the vector. 
   
int lastIndexOf(Object element)Returns the index of the last occurrence of element. If the object is not in the 
  vector, –1 is returned. 
   
int lastIndexOf(Object element,Returns the index of the last occurrence of element before start. If the object 
int start)is not in that portion of the vector, –1 is returned. 
   
void removeAllElements( )Empties the vector. After this method executes, the size of the vector is zero. 
   
boolean removeElement(Object element)Removes element from the vector. If more than one instance of the specified 
  object exists in the vector, then it is the first one that is removed. Returns 
  true if successful and false if the object is not found. 
   
void removeElementAt(int index)Removes the element at the location specified by index. 
   
void setElementAt(E element,The location specified by index is assigned element. 
 int index)  
   
void setSize(int size)Sets the number of elements in the vector to size. If the new size is less than 
  the old size, elements are lost. If the new size is larger than the old size, null 
  elements are added. 
    
int size( ) Returns the number of elements currently in the vector. 
    
String toString( ) Returns the string equivalent of the vector. 
    
void trimToSize( ) Sets the vector’s capacity equal to the number of elements that 
  it currently holds. 

The Legacy Methods Defined by Vector

Demonstrate various Vector operations

import java.util.*;

class VectorDemo {

public static void main(String args[]) {

// initial size is 3, increment is 2
Vector<Integer> v = new Vector<Integer>(3, 2);

System.out.println("Initial size: " + v.size());

System.out.println("Initial capacity: " + v.capacity());

v.addElement(1);

v.addElement(2);

v.addElement(3);

v.addElement(4);

System.out.println("Capacity after four additions: " + v.capacity());

v.addElement(5); System.out.println("Current capacity: " +

v.capacity());

v.addElement(6);

v.addElement(7);

System.out.println("Current capacity: " + v.capacity());

v.addElement(9);

v.addElement(10);

System.out.println("Current capacity: " + v.capacity());

v.addElement(11);

v.addElement(12);

System.out.println("First element: " + v.firstElement());

System.out.println("Last element: " + v.lastElement());

if(v.contains(3)) System.out.println("Vector contains 3.");

//Enumerate the elements in the vector. Enumeration vEnum = v.elements();

System.out.println("\nElements in vector:"); while(vEnum.hasMoreElements())

System.out.print(vEnum.nextElement() + " "); System.out.println();

}

}

The output from this program is shown here:

Initial size: 0

Initial capacity: 3

Capacity after four additions: 5

Current capacity: 5

Current capacity: 7

Current capacity: 9

First element: 1

Last element: 12

Vector contains 3.

Elements in vector:

1 2 3 4 5 6 7 9 10 11 12

Leave a Comment