Introduction
Vector implements a dynamic array. It is similar to ArrayList, but with two differences −
-
Vector is synchronized.
-
Vector contains many legacy methods that are not part of the collections framework.
Vector proves to be very useful if you don”t know the size of the array in advance or you just need one that can change sizes over the lifetime of a program.
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
Sr.No. | Constructor & Description |
---|---|
1 |
Vector() 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. |
2 |
Vector(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. |
3 |
Vector(int initialCapacity) This constructor is used to create an empty vector with the specified initial capacity and with its capacity increment equal to zero. |
4 |
Vector(int initialCapacity, int capacityIncrement) This constructor is used to create an empty vector with the specified initial capacity and capacity increment. |
Class methods
Sr.No. | Method & Description |
---|---|
1 |
This method appends the specified element to the end of this Vector. |
2 |
This method appends all of the elements in the specified Collection to the end of this Vector. |
3 |
This method adds the specified component to the end of this vector, increasing its size by one. |
4 |
This method returns the current capacity of this vector. |
5 |
This method removes all of the elements from this vector. |
6 |
This method returns a clone of this vector. |
7 |
This method returns true if this vector contains the specified element. |
8 |
This method returns true if this Vector contains all of the elements in the specified Collection. |
9 |
This method copies the components of this vector into the specified array. |
10 |
This method returns the component at the specified index. |
11 |
This method returns an enumeration of the components of this vector. |
12 |
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. |
13 |
This method compares the specified Object with this Vector for equality. |
14 |
This method returns the first component (the item at index 0) of this vector. |
15 |
This method performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. |
16 |
This method returns the element at the specified position in this Vector. |
17 |
This method returns the hash code value for this Vector. |
18 |
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. |
19 |
This method inserts the specified object as a component in this vector at the specified index. |
20 |
This method tests if this vector has no components. |
21 |
This method returns an iterator over the elements in this list in proper sequence. |
22 |
This method returns the last component of the vector. |
23 |
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. |
24 |
This method returns a list iterator over the elements in this list (in proper sequence). |
25 |
This method removes the element at the specified position in this Vector. |
26 |
This method removes from this Vector all of its elements that are contained in the specified Collection. |
27 |
This method removes all components from this vector and sets its size to zero. |
28 |
This method removes the first occurrence of the argument from this vector. |
29 |
This method deletes the component at the specified index. |
30 |
Removes all of the elements of this collection that satisfy the given predicate. |
31 |
This method retains only the elements in this Vector that are contained in the specified Collection. |
32 |
This method replaces the element at the specified position in this Vector with the specified element. |
33 |
This method sets the component at the specified index of this vector to be the specified object. |
34 |
This method sets the size of this vector. |
35 |
This method returns the number of components in this vector. |
36 |
Creates a late-binding and fail-fast Spliterator over the elements in this list. |
37 |
This method returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive. |
38 |
This method returns an array containing all of the elements in this Vector in the correct order. |
39 |
This method returns a string representation of this Vector, containing the String representation of each element. |
40 |
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
Adding Elements and Iterating a Vector Example
The following program illustrates several of the methods supported by Vector collection −
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(); } }
This will produce the following result −
Output
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