Java Vector Class
The Vector class implements a dynamic array. It is very similar to ArrayList, but the two are different:
| Feature | Vector | ArrayList |
|---|---|---|
| Synchronization | Synchronized (thread-safe) | Not synchronized |
| Performance | Slower | Faster |
| Capacity Increment | Can be specified | Fixed (50%) |
| iterator | fail-fast | fail-fast |
| Version Introduced | Java 1.0 | Java 1.2 |
Characteristics:
-
Synchronized: Vector is thread-safe, and all its methods are synchronized
-
dynamic resizing: Automatically grows when the number of elements exceeds the current capacity
-
Legacy Class: Although still available, ArrayList is usually recommended
-
Implements the RandomAccess interface: Supports fast random access
Vector is mainly used when the size of the array is not known in advance, or when you simply need an array that can change size.
The Vector class supports 4 constructors.
The first constructor creates a default vector with a default size of 10:
Vector()
The second constructor creates a vector of the specified size.
Vector(int size)
The third constructor creates a vector of the specified size, with the increment specified by incr. The increment indicates the number of elements the vector increases by each time.
Vector(int size,int incr)
The fourth constructor creates a vector containing the elements of collection c:
Vector(Collection c)
In addition to the methods inherited from the parent class, Vector also defines the following methods:
common operations
Example
// Add elements
boolean
add
(
E e
)
// Add an element to the end
void
add
(
int
index, E element
)
// Insert an element at the specified position
boolean
addAll
(
Collection
c ) // Add all elements in the collection // Get an element E get ( int index ) // Get the element at the specified position E firstElement ( ) // Get the first element E lastElement ( ) // Get the last element // Remove an element E remove ( int index ) // Remove and return the element at the specified position boolean remove ( Object o ) // Remove the first occurrence of the specified element void clear ( ) // Remove all elements // Other operations int size ( ) // Return the number of elements int capacity ( ) // Return the current capacity void ensureCapacity ( int minCapacity ) // Ensure at least the minimum capacity void trimToSize ( ) // Adjust the capacity to the current size boolean contains ( Object o ) // Check whether it contains the specified element int indexOf ( Object o ) // Return the index of the first occurrence of the element | No. | Method Description | | --- | --- | | 1 | [void add(int index, Object element)](/java/java-vector-addint-index-object-element.html) Inserts the specified element at the specified position in this vector. | | 2 | [boolean add(Object o)](/java/java-vector-add.html) Appends the specified element to the end of this vector. | | 3 | [boolean addAll(Collection c)](/java/java-vector-addall.html) Appends all elements in the specified Collection to the end of this vector, in the order returned by the specified collection's iterator. | | 4 | [boolean addAll(int index, Collection c)](/java/java-vector-addallint-index-collection-c.html) Inserts all elements in the specified Collection into this vector at the specified position. | | 5 | [void addElement(Object obj)](/java/java-vector-addelement.html) Adds the specified component to the end of this vector, increasing its size by 1. | | 6 | [int capacity()](/java/java-vector-capacity.html) Returns the current capacity of this vector. | | 7 | [void clear()](/java/java-vector-clear.html) Removes all elements from this vector. | | 8 | [Object clone()](/java/java-vector-clone.html) Returns a copy of the vector. | | 9 | [boolean contains(Object elem)](/java/java-vector-contains.html) Returns true if this vector contains the specified element. | | 10 | [boolean containsAll(Collection c)](/java/java-vector-containsall.html) Returns true if this vector contains all elements in the specified Collection. | | 11 | [void copyInto(Object[] anArray)](/java/java-vector-copyinto.html) Copies the components of this vector into the specified array. | | 12 | [Object elementAt(int index)](/java/java-vector-elementat.html) Returns the component at the specified index. | | 13 | [Enumeration elements()](/java/java-vector-elements.html) Returns an enumeration of the components of this vector. | | 14 | [void ensureCapacity(int minCapacity)](/java/java-vector-ensurecapacity.html) 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. | | 15 | [boolean equals(Object o)](/java/java-vector-equals.html) Compares the specified object with this vector for equality. | | 16 | [Object firstElement()](/java/java-vector-firstelement.html) Returns the first component of this vector (the item at index 0). | | 17 | [Object get(int index)](/java/java-vector-get.html) Returns the element at the specified position in the vector. | | 18 | [int hashCode()](/java/java-vector-hashcode.html) Returns the hash code value for this vector. | | 19 | [int indexOf(Object elem)](/java/java-vector-indexof.html) Returns the index of the first occurrence of the specified element in this vector, or -1 if this vector does not contain the element. | | 20 | [int indexOf(Object elem, int index)](/java/java-vector-indexofobject-elem-int-index.html) Returns the index of the first occurrence of the specified element in this vector, searching forward from index, or -1 if the element is not found. | | 21 | [void insertElementAt(Object obj, int index)](/java/java-vector-insertelementat.html) Inserts the specified object as a component in this vector at the specified index. | | 22 | [boolean isEmpty()](/java/java-vector-isempty.html) Tests whether this vector has no components. | | 23 | [Object lastElement()](/java/java-vector-lastelement.html) Returns the last component of this vector. | | 24 | [int lastIndexOf(Object elem)](/java/java-vector-lastindexofobject.html) Returns the index of the last occurrence of the specified element in this vector; returns -1 if this vector does not contain the element. | | 25 | [int lastIndexOf(Object elem, int index)](/java/java-vector-lastindexofobject-elem-int-index.html) Returns the index of the last occurrence of the specified element in this vector, searching backward from index, or -1 if the element is not found. | | 26 | [Object remove(int index)](/java/java-vector-remove.html) Removes the element at the specified position in this vector. | | 27 | [boolean remove(Object o)](/java/java-vector-remove.html) Removes the first match of the specified element from this vector; if the vector does not contain the element, it is unchanged. | | 28 | [boolean removeAll(Collection c)](/java/java-vector-removeall.html) Removes from this vector all elements contained in the specified Collection. | | 29 | [void removeAllElements()](/java/java-vector-removeallelements.html) Removes all components from this vector and sets its size to zero. | | 30 | [boolean removeElement(Object obj)](/java/java-vector-removeelement.html) Removes the first (lowest-indexed) match of the variable from this vector. | | 31 | [void removeElementAt(int index)](/java/java-vector-removeelementat.html) Deletes the component at the specified index. | | 32 | [protected void removeRange(int fromIndex, int toIndex)](/java/java-vector-removerange.html) Removes from this List all elements whose indexes are between fromIndex (inclusive) and toIndex (exclusive). | | 33 | [boolean retainAll(Collection c)](/java/java-vector-retainall.html) Retains only the elements in this vector that are contained in the specified Collection. | | 34 | [Object set(int index, Object element)](/java/java-vector-set.html) Replaces the element at the specified position in this vector with the specified element. | | 35 | [void setElementAt(Object obj, int index)](/java/java-vector-setelementat.html) Sets the component at the specified index of this vector to the specified object. | | 36 | [void setSize(int newSize)](/java/java-vector-setsize.html) Sets the size of this vector. | | 37 | [int size()](/java/java-vector-size.html) Returns the number of components in this vector. | | 38 | [List subList(int fromIndex, int toIndex)](/java/java-vector-sublist.html) Returns a view of the portion of this List with elements ranging from fromIndex (inclusive) to toIndex (exclusive). | | 39 | [Object[] toArray()](/java/java-vector-toarray.html) Returns an array containing all the elements in this vector in the correct order. | | 40 | [Object[] toArray(Object[] a)](/java/java-vector-toarray.html) Returns an array containing all the elements in this vector in the correct order; the runtime type of the returned array is that of the specified array. | | 41 | [String toString()](/java/java-vector-tostring.html) Returns the string representation of this vector, containing the String representation of each element. | | 42 | [void trimToSize()](/java/java-vector-trimtosize.html) Trims the capacity of this vector to be equal to the current size of the vector. | ### Example The following program demonstrates several of the methods supported by this collection: ## Example import java.util.* ; public class VectorDemo { public static void main ( String args [ ] ) { // initial size is 3, increment is 2 Vector v = new Vector ( 3 , 2 ) ; System . out . println ( "Initial size: " + v. size ( ) ) ; System . out . println ( "Initial capacity: " + v. capacity ( ) ) ; v. addElement ( new Integer ( 1 ) ) ; v. addElement ( new Integer ( 2 ) ) ; v. addElement ( new Integer ( 3 ) ) ; v. addElement ( new Integer ( 4 ) ) ; System . out . println ( "Capacity after four additions: " + v. capacity ( ) ) ; v. addElement ( new Double ( 5.45 ) ) ; System . out . println ( "Current capacity: " + v. capacity ( ) ) ; v. addElement ( new Double ( 6.08 ) ) ; v. addElement ( new Integer ( 7 ) ) ; System . out . println ( "Current capacity: " + v. capacity ( ) ) ; v. addElement ( new Float ( 9.4 ) ) ; v. addElement ( new Integer ( 10 ) ) ; System . out . println ( "Current capacity: " + v. capacity ( ) ) ; v. addElement ( new Integer ( 11 ) ) ; v. addElement ( new Integer ( 12 ) ) ; System . out . println ( "First element: " + ( Integer ) v. firstElement ( ) ) ; System . out . println ( "Last element: " + ( Integer ) v. lastElement ( ) ) ; if ( v. contains ( new Integer ( 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 compilation and execution result of the above example is as follows: ``` 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.45 6.08 7 9.4 10 11 12 ``` [Java Data Structures](/java/java-data-structures.html)