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.
Each vector tries to optimize storage management by maintaining a
capacity and a capacityIncrement. The
capacity is always at least as large as the vector
size; it is usually larger because as components are added to the
vector, the vector's storage increases in chunks the size of
capacityIncrement. An application can increase the
capacity of a vector before inserting a large number of
components; this reduces the amount of incremental reallocation.
The iterators returned by this class's iterator and
listIterator methods are fail-fast:
if the vector is structurally modified at any time after the iterator is
created, in any way except through the iterator's own
remove or
add methods, the iterator will throw a
ConcurrentModificationException. Thus, in the face of
concurrent modification, the iterator fails quickly and cleanly, rather
than risking arbitrary, non-deterministic behavior at an undetermined
time in the future. The Enumerations returned by
the elements method are not fail-fast; if the
Vector is structurally modified at any time after the enumeration is
created then the results of enumerating are undefined.
Note that the fail-fast behavior of an iterator cannot be guaranteed
as it is, generally speaking, impossible to make any hard guarantees in the
presence of unsynchronized concurrent modification. Fail-fast iterators
throw ConcurrentModificationException on a best-effort basis.
Therefore, it would be wrong to write a program that depended on this
exception for its correctness: the fail-fast behavior of iterators
should be used only to detect bugs.
As of the Java 2 platform v1.2, this class was retrofitted to
implement the List interface, making it a member of the
Java Collections Framework. Unlike the new collection
implementations, Vector is synchronized. If a thread-safe
implementation is not needed, it is recommended to use ArrayList in place of Vector.
Collection, LinkedList
| Modifier and Type | Class and Description |
|---|---|
| private class | Vector.
An optimized version of AbstractList.Itr |
| pack-priv class | Vector.
An optimized version of AbstractList.ListItr |
| pack-priv class | Vector.
Similar to ArrayList Spliterator |
| Modifier and Type | Field and Description |
|---|---|
| protected int | capacityIncrement
The amount by which the capacity of the vector is automatically incremented when its size becomes greater than its capacity. |
| protected int | elementCount
The number of valid components in this |
| protected Object[] | elementData
The array buffer into which the components of the vector are stored. |
| private static final long | serialVersionUID
use serialVersionUID from JDK 1.0.2 for interoperability |
| Access | Constructor and Description |
|---|---|
| public | Vector(int
the initial capacity of the vector initialCapacity, int the amount by which the capacity is
increased when the vector overflows capacityIncrement)Constructs an empty vector with the specified initial capacity and capacity increment. |
| public | Vector(int
the initial capacity of the vector initialCapacity)Constructs an empty vector with the specified initial capacity and with its capacity increment equal to zero. |
| public | Vector()
Constructs an empty vector so that its internal data array
has size |
| public | Vector(Collection<? extends E>
the collection whose elements are to be placed into this
vector c)Constructs a vector containing the elements of the specified collection, in the order they are returned by the collection's iterator. |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| public synchronized boolean | add(E
element to be appended to this Vector e)Overrides java. Implements java. Appends the specified element to the end of this Vector. |
| public void | add(int
index at which the specified element is to be inserted index, E element to be inserted element)Overrides java. Implements java. Inserts the specified element at the specified position in this Vector. |
| public boolean | Returns: true if this Vector changed as a result of the callelements to be inserted into this Vector c)Overrides java. Implements java. 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. |
| public synchronized boolean | Returns: true if this Vector changed as a result of the callindex at which to insert the first element from the
specified collection index, Collection<? extends E> elements to be inserted into this Vector c)Overrides java. Implements java. Inserts all of the elements in the specified Collection into this Vector at the specified position. |
| public synchronized void | addElement(E
the component to be added obj)Adds the specified component to the end of this vector, increasing its size by one. |
| private synchronized boolean | |
| public synchronized int | Returns: the current capacity (the length of its internal data array, kept in the fieldelementData
of this vector)Returns the current capacity of this vector. |
| pack-priv void | |
| public void | clear()
Overrides java. Implements java. Removes all of the elements from this Vector. |
| public synchronized Object | Returns: a clone of this vectorOverrides java. Returns a clone of this vector. |
| public boolean | Returns: true if this vector contains the specified elementelement whose presence in this vector is to be tested o)Overrides java. Implements java. Returns |
| public synchronized boolean | Returns: true if this Vector contains all of the elements in the specified collectiona collection whose elements will be tested for containment
in this Vector c)Overrides java. Implements java. Returns true if this Vector contains all of the elements in the specified Collection. |
| public synchronized void | |
| public synchronized E | Returns: the component at the specified indexan index into this vector index)Returns the component at the specified index. |
| pack-priv static <E> E | |
| pack-priv E | |
| public Enumeration | Returns: an enumeration of the components of this vectorReturns an enumeration of the components of this vector. |
| public synchronized void | ensureCapacity(int
the desired minimum capacity minCapacity)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. |
| public synchronized boolean | Returns: true if the specified Object is equal to this Vectorthe Object to be compared for equality with this Vector o)Overrides java. Implements java. Compares the specified Object with this Vector for equality. |
| public synchronized E | Returns: the first component of this vectorReturns the first component (the item at index |
| public synchronized void | forEach(Consumer<? super E> action)
Overrides default java. Performs the given action for each element of the |
| public synchronized E | Returns: object at the specified indexindex of the element to return index)Implements abstract java. Implements java. Returns the element at the specified position in this Vector. |
| private Object[] | grow(int
the desired minimum capacity minCapacity)Increases the capacity to ensure that it can hold at least the number of elements specified by the minimum capacity argument. |
| private Object[] | |
| public synchronized int | hashCode()
Overrides java. Implements java. Returns the hash code value for this Vector. |
| public int | Returns: the index of the first occurrence of the specified element in this vector, or -1 if this vector does not contain the elementelement to search for o)Overrides java. Implements java. Returns the index of the first occurrence of the specified element in this vector, or -1 if this vector does not contain the element. |
| public synchronized int | Returns: the index of the first occurrence of the element in this vector at positionindex or later in the vector;
-1 if the element is not found.element to search for o, int index to start searching from index)Returns the index of the first occurrence of the specified element in
this vector, searching forwards from |
| public synchronized void | insertElementAt(E
the component to insert obj, int where to insert the new component index)Inserts the specified object as a component in this vector at the
specified |
| private static boolean | |
| public synchronized boolean | Returns: true if and only if this vector has
no components, that is, its size is zero;
false otherwise.Overrides java. Implements java. Tests if this vector has no components. |
| public synchronized Iterator | Returns: an iterator over the elements in this list in proper sequenceOverrides java. Implements java. Returns an iterator over the elements in this list in proper sequence. |
| public synchronized E | Returns: the last component of the vector, i.e., the component at indexsize() - 1Returns the last component of the vector. |
| public synchronized int | Returns: the index of the last occurrence of the specified element in this vector, or -1 if this vector does not contain the elementelement to search for o)Overrides java. Implements java. Returns the index of the last occurrence of the specified element in this vector, or -1 if this vector does not contain the element. |
| public synchronized int | Returns: the index of the last occurrence of the element at position less than or equal toindex in this vector;
-1 if the element is not found.element to search for o, int index to start searching backwards from index)Returns the index of the last occurrence of the specified element in
this vector, searching backwards from |
| public synchronized ListIterator | listIterator(int index)
Overrides java. Implements java. Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. |
| public synchronized ListIterator | listIterator()
Overrides java. Implements java. Returns a list iterator over the elements in this list (in proper sequence). |
| private static long[] | |
| private void | readObject(ObjectInputStream
the stream in)Loads a |
| public boolean | Returns: true if the Vector contained the specified elementelement to be removed from this Vector, if present o)Overrides java. Implements java. Removes the first occurrence of the specified element in this Vector If the Vector does not contain the element, it is unchanged. |
| public synchronized E | Returns: element that was removedthe index of the element to be removed index)Overrides java. Implements java. Removes the element at the specified position in this Vector. |
| public boolean | Returns: true if this Vector changed as a result of the calla collection of elements to be removed from the Vector c)Overrides java. Implements java. Removes from this Vector all of its elements that are contained in the specified Collection. |
| public synchronized void | |
| public synchronized boolean | Returns: true if the argument was a component of this
vector; false otherwise.the component to be removed obj)Removes the first (lowest-indexed) occurrence of the argument from this vector. |
| public synchronized void | removeElementAt(int
the index of the object to remove index)Deletes the component at the specified index. |
| public boolean | removeIf(Predicate<? super E> filter)
Overrides default java. Removes all of the elements of this collection that satisfy the given predicate (optional operation). |
| protected synchronized void | removeRange(int fromIndex, int toIndex)
Overrides java. Removes from this list all of the elements whose index is between
|
| public synchronized void | replaceAll(UnaryOperator<E> operator)
Overrides default java. Replaces each element of this list with the result of applying the operator to that element (optional operation). |
| public boolean | Returns: true if this Vector changed as a result of the calla collection of elements to be retained in this Vector
(all other elements are removed) c)Overrides java. Implements java. Retains only the elements in this Vector that are contained in the specified Collection. |
| public synchronized E | Returns: the element previously at the specified positionindex of the element to replace index, E element to be stored at the specified position element)Overrides java. Implements java. Replaces the element at the specified position in this Vector with the specified element. |
| private static void | |
| public synchronized void | setElementAt(E
what the component is to be set to obj, int the specified index index)Sets the component at the specified |
| public synchronized void | |
| private void | shiftTailOverGap(Object[] es, int lo, int hi)
Erases the gap from lo to hi, by sliding down following elements. |
| public synchronized int | Returns: the number of components in this vectorImplements abstract java. Implements java. Returns the number of components in this vector. |
| public synchronized void | sort(Comparator<? super E> c)
Overrides default java. Sorts this list according to the order induced by the specified
|
| public Spliterator | Returns: aSpliterator over the elements in this listOverrides default java. Creates a late-binding
and fail-fast |
| public synchronized List | Returns: a view of the specified range within this Listlow endpoint (inclusive) of the subList fromIndex, int high endpoint (exclusive) of the subList toIndex)Overrides java. Implements java. Returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive. |
| public synchronized Object[] | toArray()
Overrides java. Implements java. Returns an array containing all of the elements in this Vector in the correct order. |
| public synchronized < type of array elements. The same type as T> T[]<E> or a
supertype of <E>. | Returns: an array containing the elements of the Vectorthe array into which the elements of the Vector are to
be stored, if it is big enough; otherwise, a new array of the
same runtime type is allocated for this purpose. a)Overrides java. Implements java. 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. |
| public synchronized String | toString()
Overrides java. Returns a string representation of this Vector, containing the String representation of each element. |
| public synchronized void | |
| private void | writeObject(ObjectOutputStream
the stream s)Saves the state of the |