APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.util

public Class Vector<E>

extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.lang.Cloneable, java.util.RandomAccess, java.util.List, java.util.SequencedCollection, java.util.Collection, java.lang.Iterable
Known Direct Subclasses
java.util.Stack
Type Parameters
<E>
Type of component elements
Imports
java.io.IOException, .ObjectInputStream, .StreamCorruptedException, java.util.function.Consumer, .Predicate, .UnaryOperator, jdk.internal.util.ArraysSupport

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.

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.

Authors
Lee Boynton, Jonathan Payne
Since
1.0
See Also
Collection, LinkedList

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
Vector.Itr

An optimized version of AbstractList.Itr

pack-priv class
Vector.ListItr

An optimized version of AbstractList.ListItr

pack-priv class
Vector.VectorSpliterator

Similar to ArrayList Spliterator

Field Summary

Modifier and TypeField 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 Vector object.

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

Inherited from java.util.AbstractList:
modCount

Constructor Summary

AccessConstructor 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 10 and its standard capacity increment is zero.

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.

Method Summary

Modifier and TypeMethod and Description
private void
add(E e, Object[] elementData, int s)

This helper method split out from add(E) to keep method bytecode size under 35 (the -XX:MaxInlineSize default value), which helps when add(E) is called in a C1-compiled loop.

public synchronized boolean

Returns:

true (as specified by Collection#add)
add
(E
element to be appended to this Vector
e
)

Overrides java.util.AbstractList.add.

Implements java.util.List.add, java.util.Collection.add.

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.util.AbstractList.add.

Implements java.util.List.add.

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

public boolean

Returns:

true if this Vector changed as a result of the call
addAll
(Collection<? extends E>
elements to be inserted into this Vector
c
)

Overrides java.util.AbstractCollection.addAll.

Implements java.util.List.addAll, java.util.Collection.addAll.

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 call
addAll
(int
index 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.util.AbstractList.addAll.

Implements java.util.List.addAll.

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
bulkRemove(Predicate<? super E> filter)

public synchronized int

Returns:

the current capacity (the length of its internal data array, kept in the field elementData of this vector)
capacity
()

Returns the current capacity of this vector.

pack-priv void
public void
clear()

Overrides java.util.AbstractList.clear.

Implements java.util.List.clear, java.util.Collection.clear.

Removes all of the elements from this Vector.

public synchronized Object

Returns:

a clone of this vector
clone
()

Overrides java.lang.Object.clone.

Returns a clone of this vector.

public boolean

Returns:

true if this vector contains the specified element
contains
(Object
element whose presence in this vector is to be tested
o
)

Overrides java.util.AbstractCollection.contains.

Implements java.util.List.contains, java.util.Collection.contains.

Returns true if this vector contains the specified element.

public synchronized boolean

Returns:

true if this Vector contains all of the elements in the specified collection
containsAll
(Collection<?>
a collection whose elements will be tested for containment in this Vector
c
)

Overrides java.util.AbstractCollection.containsAll.

Implements java.util.List.containsAll, java.util.Collection.containsAll.

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

public synchronized void
copyInto(Object[]
the array into which the components get copied
anArray
)

Copies the components of this vector into the specified array.

public synchronized E

Returns:

the component at the specified index
elementAt
(int
an index into this vector
index
)

Returns the component at the specified index.

pack-priv static <E> E
elementAt(Object[] es, int index)

pack-priv E
elementData(int index)

public Enumeration<E>

Returns:

an enumeration of the components of this vector
elements
()

Returns 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 Vector
equals
(Object
the Object to be compared for equality with this Vector
o
)

Overrides java.util.AbstractList.equals.

Implements java.util.List.equals, java.util.Collection.equals.

Compares the specified Object with this Vector for equality.

public synchronized E

Returns:

the first component of this vector
firstElement
()

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

public synchronized void
forEach(Consumer<? super E> action)

Overrides default java.lang.Iterable.forEach.

Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

public synchronized E

Returns:

object at the specified index
get
(int
index of the element to return
index
)

Implements abstract java.util.AbstractList.get.

Implements java.util.List.get.

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[]
grow()

public synchronized int
hashCode()

Overrides java.util.AbstractList.hashCode.

Implements java.util.List.hashCode, java.util.Collection.hashCode.

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 element
indexOf
(Object
element to search for
o
)

Overrides java.util.AbstractList.indexOf.

Implements java.util.List.indexOf.

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 position index or later in the vector; -1 if the element is not found.
indexOf
(Object
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 index, or returns -1 if the element is not found.

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 index.

private static boolean
isClear(long[] bits, int i)

public synchronized boolean

Returns:

true if and only if this vector has no components, that is, its size is zero; false otherwise.
isEmpty
()

Overrides java.util.AbstractCollection.isEmpty.

Implements java.util.List.isEmpty, java.util.Collection.isEmpty.

Tests if this vector has no components.

public synchronized Iterator<E>

Returns:

an iterator over the elements in this list in proper sequence
iterator
()

Overrides java.util.AbstractList.iterator.

Implements java.util.List.iterator, java.util.Collection.iterator.

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 index size() - 1
lastElement
()

Returns 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 element
lastIndexOf
(Object
element to search for
o
)

Overrides java.util.AbstractList.lastIndexOf.

Implements java.util.List.lastIndexOf.

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 to index in this vector; -1 if the element is not found.
lastIndexOf
(Object
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 index, or returns -1 if the element is not found.

public synchronized ListIterator<E>
listIterator(int index)

Overrides java.util.AbstractList.listIterator.

Implements java.util.List.listIterator.

Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.

public synchronized ListIterator<E>
listIterator()

Overrides java.util.AbstractList.listIterator.

Implements java.util.List.listIterator.

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

private static long[]
nBits(int n)

private void
readObject(ObjectInputStream
the stream
in
)

Loads a Vector instance from a stream (that is, deserializes it).

public boolean

Returns:

true if the Vector contained the specified element
remove
(Object
element to be removed from this Vector, if present
o
)

Overrides java.util.AbstractCollection.remove.

Implements java.util.List.remove, java.util.Collection.remove.

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 removed
remove
(int
the index of the element to be removed
index
)

Overrides java.util.AbstractList.remove.

Implements java.util.List.remove.

Removes the element at the specified position in this Vector.

public boolean

Returns:

true if this Vector changed as a result of the call
removeAll
(Collection<?>
a collection of elements to be removed from the Vector
c
)

Overrides java.util.AbstractCollection.removeAll.

Implements java.util.List.removeAll, java.util.Collection.removeAll.

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

public synchronized void
removeAllElements()

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

public synchronized boolean

Returns:

true if the argument was a component of this vector; false otherwise.
removeElement
(Object
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.util.Collection.removeIf.

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.util.AbstractList.removeRange.

Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.

public synchronized void
replaceAll(UnaryOperator<E> operator)

Overrides default java.util.List.replaceAll.

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 call
retainAll
(Collection<?>
a collection of elements to be retained in this Vector (all other elements are removed)
c
)

Overrides java.util.AbstractCollection.retainAll.

Implements java.util.List.retainAll, java.util.Collection.retainAll.

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

public synchronized E

Returns:

the element previously at the specified position
set
(int
index of the element to replace
index
,
E
element to be stored at the specified position
element
)

Overrides java.util.AbstractList.set.

Implements java.util.List.set.

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

private static void
setBit(long[] bits, int i)

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 index of this vector to be the specified object.

public synchronized void
setSize(int
the new size of this vector
newSize
)

Sets the size of this vector.

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 vector
size
()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.List.size, java.util.Collection.size.

Returns the number of components in this vector.

public synchronized void
sort(Comparator<? super E> c)

Overrides default java.util.List.sort.

Sorts this list according to the order induced by the specified Comparator (optional operation).

public Spliterator<E>

Returns:

a Spliterator over the elements in this list
spliterator
()

Overrides default java.util.List.spliterator, java.util.Collection.spliterator.

Creates a late-binding and fail-fast Spliterator over the elements in this list.

public synchronized List<E>

Returns:

a view of the specified range within this List
subList
(int
low endpoint (inclusive) of the subList
fromIndex
,
int
high endpoint (exclusive) of the subList
toIndex
)

Overrides java.util.AbstractList.subList.

Implements java.util.List.subList.

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

public synchronized Object[]
toArray()

Overrides java.util.AbstractCollection.toArray.

Implements java.util.List.toArray, java.util.Collection.toArray.

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 <E> or a supertype of <E>.
T
>
T[]

Returns:

an array containing the elements of the Vector
toArray
(T[]
the 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.util.AbstractCollection.toArray.

Implements java.util.List.toArray, java.util.Collection.toArray.

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.util.AbstractCollection.toString.

Returns a string representation of this Vector, containing the String representation of each element.

public synchronized void
trimToSize()

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

private void
writeObject(ObjectOutputStream
the stream
s
)

Saves the state of the Vector instance to a stream (that is, serializes it).

Inherited from java.util.AbstractList:
subListRangeCheck