Java.util.Vector Class

Advertisements


Introduction

The java.util.Vector class implements a growable array of objects. Similar to an Array, it contains components that can be accessed using an integer index. Following are the important points about Vector:

  • The size of a Vector can grow or shrink as needed to accommodate adding and removing items.

  • Each vector tries to optimize storage management by maintaining a capacity and a capacityIncrement.

  • As of the Java 2 platform v1.2, this class was retrofitted to implement the List interface.

  • Unlike the new collection implementations, Vector is synchronized.

  • This class is a member of the Java Collections Framework.

Class declaration

Following is the declaration for java.util.Vector class:

public class Vector<E>
   extends AbstractList<E>
      implements List<E>, RandomAccess, Cloneable, Serializable

Here <E> represents an Element, which could be any class. For example, if you're building an array list of Integers then you'd initialize it as follows:

ArrayList<Integer> list = new ArrayList<Integer>();  

Class constructors

S.N.Constructor & Description
1Vector()
This constructor is used to create an empty vector so that its internal data array has size 10 and its standard capacity increment is zero.
2Vector(Collection<? extends E> c)
This constructor is used to create a vector containing the elements of the specified collection, in the order they are returned by the collection's iterator.
3Vector(int initialCapacity)
This constructor is used to create an empty vector with the specified initial capacity and with its capacity increment equal to zero.
4Vector(int initialCapacity, int capacityIncrement)
This constructor is used to create an empty vector with the specified initial capacity and capacity increment.

Class methods

S.N.Method & Description
1boolean add(E e)
This method appends the specified element to the end of this Vector.
2void add(int index, E element)
This method inserts the specified element at the specified position in this Vector.
3boolean addAll(Collection<? extends E> c)
This method appends all of the elements in the specified Collection to the end of this Vector.
4boolean addAll(int index, Collection<? extends E> c)
This method inserts all of the elements in the specified Collection into this Vector at the specified position.
5void addElement(E obj)
This method adds the specified component to the end of this vector, increasing its size by one.
6int capacity()
This method returns the current capacity of this vector.
7void clear()
This method removes all of the elements from this vector.
8clone clone()
This method returns a clone of this vector.
9boolean contains(Object o)
This method returns true if this vector contains the specified element.
10boolean containsAll(Collection<?> c)
This method returns true if this Vector contains all of the elements in the specified Collection.
11void copyInto(Object[ ] anArray)
This method copies the components of this vector into the specified array.
12E elementAt(int index)
This method returns the component at the specified index.
13Enumeration<E> elements()
This method returns an enumeration of the components of this vector.
14void ensureCapacity(int minCapacity)
This method 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.
15boolean equals(Object o)
This method compares the specified Object with this Vector for equality.
16E firstElement()
This method returns the first component (the item at index 0) of this vector.
17E get(int index)
This method returns the element at the specified position in this Vector.
18int hashCode()
This method returns the hash code value for this Vector.
19int indexOf(Object o)
This method returns the index of the first occurrence of the specified element in this vector, or -1 if this vector does not contain the element.
20int indexOf(Object o, int index)
This method returns the index of the first occurrence of the specified element in this vector, searching forwards from index, or returns -1 if the element is not found.
21void insertElementAt(E obj, int index)
This method inserts the specified object as a component in this vector at the specified index.
22boolean isEmpty()
This method tests if this vector has no components.
23E lastElement()
This method returns the last component of the vector.
24int lastIndexOf(Object o)
This method returns the index of the last occurrence of the specified element in this vector, or -1 if this vector does not contain the element.
25int lastIndexOf(Object o, int index)
This method 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.
26E remove(int index)
This method removes the element at the specified position in this Vector.
27boolean remove(Object o)
This method removes the first occurrence of the specified element in this Vector If the Vector does not contain the element, it is unchanged.
28boolean removeAll(Collection<?> c)
This method removes from this Vector all of its elements that are contained in the specified Collection.
29void removeAllElements()
This method removes all components from this vector and sets its size to zero.
30boolean removeElement(Object obj)
This method removes the first occurrence of the argument from this vector.
31void removeElementAt(int index)
This method deletes the component at the specified index.
32protected void removeRange(int fromIndex, int toIndex)
This method removes from this List all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive.
33boolean retainAll(Collection<?> c)
This method retains only the elements in this Vector that are contained in the specified Collection.
34E set(int index, E element)
This method replaces the element at the specified position in this Vector with the specified element.
35void setElementAt(E obj, int index)
This method sets the component at the specified index of this vector to be the specified object.
36void setSize(int newSize)
This method sets the size of this vector.
37int size()
This method returns the number of components in this vector.
38List <E> subList(int fromIndex, int toIndex)
This method returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive.
39object[ ] toArray()
This method returns an array containing all of the elements in this Vector in the correct order.
40<T> T[ ] toArray(T[ ] a)
This method 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.
41String toString()
This method returns a string representation of this Vector, containing the String representation of each element.
42void trimToSize()
This method trims the capacity of this vector to be the vector's current size.

Methods inherited

This class inherits methods from the following classes:

  • java.util.AbstractMap

  • java.lang.Object

  • java.util.List




Advertisements
Advertisements