Java Program to Implement Vector API

This Java program Implements Vector API.The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.

Here is the source code of the Java Program to Implement Vector API.The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

importjava.util.Collection;

importjava.util.Enumeration;

importjava.util.Iterator;

importjava.util.List;

importjava.util.ListIterator;

importjava.util.Vector;

publicclass VectorImpl<E>

{

private Vector<E> vector;

/**

Constructs an empty vector so that its internal data array has size 10

and its standard capacity increment is zero.

**/

public VectorImpl()

{

vector =new Vector<E>();

}

/**

Constructs an empty vector so that its internal data array has size 10

and its standard capacity increment is zero.

**/

public VectorImpl(Collection<?extends E> c)

{

vector =new Vector<E>(c);

}

/**

Constructs an empty vector with the specified initial capacity and with

its capacity increment equal to zero

**/

public VectorImpl(int initialCapacity)

{

vector =new Vector<E>(initialCapacity);

}

/**

Constructs an empty vector with the specified initial capacity and

capacity increment.

**/

public VectorImpl(int initialCapacity, int capacityIncrement)

{

vector =new Vector<E>(initialCapacity, capacityIncrement);

}

/** Appends the specified element to the end of this Vector. **/

publicboolean add(E e)

{

return vector.add(e);

}

/** Inserts the specified element at the specified position in this Vector. **/

publicvoid add(int index, E element)

{

vector.add(index, element);

}

/**

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.

**/

publicboolean addAll(Collection<?extends E> c)

{

return vector.addAll(c);

}

/**

Inserts all of the elements in the specified Collection into this Vector

at the specified position.

**/

publicboolean addAll(int index, Collection<?extends E> c)

{

return vector.addAll(index, c);

}

/** Adds the specified component to the end of this vector, increasing its size by one. **/

publicvoid addElement(E obj)

{

vector.addElement(obj);

}

/** Returns the current capacity of this vector. **/

publicint capacity()

{

return vector.capacity();

}

/** Removes all of the elements from this Vector. **/

publicvoid clear()

{

vector.clear();

}

/** Returns a clone of this vector. **/

publicObject clone()

{

return vector.clone();

}

/** Returns true if this vector contains the specified element. **/

publicboolean contains(Object o)

{

return vector.contains(o);

}

/** Returns true if this Vector contains all of the elements in the specified Collection. **/

publicboolean containsAll(Collection<?> c)

{

return vector.containsAll(c);

}

/** Copies the components of this vector into the specified array. **/

publicvoid copyInto(Object[] anArray)

{

vector.copyInto(anArray);

}

/** Returns the component at the specified index. **/

public E elementAt(int index)

{

return vector.elementAt(index);

}

/** Returns an enumeration of the components of this vector. **/

public Enumeration<E> elements()

{

return vector.elements();

}

/**

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.

**/

publicvoid ensureCapacity(int minCapacity)

{

vector.ensureCapacity(minCapacity);

}

/** Compares the specified Object with this Vector for equality. **/

publicboolean equals(Object o)

{

return vector.equals(o);

}

/** Returns the first component (the item at index 0) of this vector. **/

public E firstElement()

{

return vector.firstElement();

}

/** Returns the element at the specified position in this Vector. **/

public E get(int index)

{

return vector.get(index);

}

/** Returns the hash code value for this Vector. **/

publicint hashCode()

{

return vector.hashCode();

}

/**

Returns the index of the first occurrence of the specified element in

this vector, or -1 if this vector does not contain the element.

**/

publicint indexOf(Object o)

{

return vector.indexOf(o);

}

/**

Returns the index of the last occurrence of the specified element in this

vector, searching backwards from index, or returns -1 if the element is

not found.

**/

publicint indexOf(Object o, int index)

{

return vector.indexOf(o, index);

}

/** Inserts the specified object as a component in this vector at the specified index. **/

publicvoid insertElementAt(E obj, int index)

{

vector.insertElementAt(obj, index);

}

/** Tests if this vector has no components. **/

publicboolean isEmpty()

{

return vector.isEmpty();

}

/** Returns an iterator over the elements in this list in proper sequence. **/

public Iterator<E> iterator()

{

return vector.iterator();

}

/** Returns the last component of the vector. **/

public E lastElement()

{

return vector.lastElement();

}

/**

Returns the index of the last occurrence of the specified element in this

vector, or -1 if this vector does not contain the element.

**/

publicint lastIndexOf(Object o)

{

return vector.lastIndexOf(o);

}

/**

Returns the index of the last occurrence of the specified element in this

vector, searching backwards from index, or returns -1 if the element is

not found.

**/

publicint lastIndexOf(Object o, int index)

{

return vector.lastIndexOf(o, index);

}

/** Returns a list iterator over the elements in this list (in proper sequence). **/

public ListIterator<E> listIterator()

{

return vector.listIterator();

}

/**

Returns a list iterator over the elements in this list (in proper sequence),

starting at the specified position in the list.

**/

public ListIterator<E> listIterator(int index)

{

return vector.listIterator(index);

}

/** Removes the element at the specified position in this Vector. **/

public E remove(int index)

{

return vector.remove(index);

}

/**

Removes the first occurrence of the specified element in this Vector If

the Vector does not contain the element, it is unchanged.

**/

publicboolean remove(Object o)

{

return vector.remove(o);

}

/**

Removes from this Vector all of its elements that are contained in the

specified Collection.

**/

publicboolean removeAll(Collection<?> c)

{

return vector.removeAll(c);

}

/** Removes all components from this vector and sets its size to zero. **/

publicvoid removeAllElements()

{

vector.removeAllElements();

}

/** Removes the first (lowest-indexed) occurrence of the argument from this vector. **/

publicboolean removeElement(Object obj)

{

return vector.removeElement(obj);

}

/** Retains only the elements in this Vector that are contained in specified Collection. **/

publicboolean retainAll(Collection<?> c)

{

return vector.removeAll(c);

}

/** Replaces the element at the specified position in this Vector with the specified element. **/

public E set(int index, E element)

{

return vector.set(index, element);

}

/** Sets the component at the specified index of this vector to be the specified object. **/

publicvoid setElementAt(E obj, int index)

{

vector.setElementAt(obj, index);

}

/** Sets the size of this vector. **/

publicvoid setSize(int newSize)

{

vector.setSize(newSize);

}

/** Returns the number of components in this vector. **/

publicint size()

{

return vector.size();

}

/** Returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive. **/

public List<E> subList(int fromIndex, int toIndex)

{

return vector.subList(fromIndex, toIndex);

}

/** Trims the capacity of this vector to be the vector's current size. **/

publicvoid trimToSize()

{

vector.trimToSize();

}

publicstaticvoid main(String... arg)

{

VectorImpl<Integer> vector =new VectorImpl<Integer>();

vector.add(100);

vector.add(200);

vector.add(399);

vector.add(120);

vector.addElement(400);

vector.addElement(40);

System.out.println("the capacity of the vector is "+ vector.capacity());

$ javac VectorImpl.java
$ java VectorImpl
the capacity of the vector is 10
elements of vector is
10020039912040040
the vector contains 200
the first element is 100
the last element is 40
element 399 at index 2 removed
the size of the vector is 5

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

If you wish to look at all Java Programming examples, go to Java Programs.