public class Vector<E>
ImplementsCollection<E>, Iterable<E>, List<E>, RandomAccess
Known subtypesStack
Vector is a variable size contiguous indexable array of objects. The size of the vector is the number of objects it contains. The capacity of the vector is the number of objects it can hold.
Objects may be inserted at any position up to the size of the vector, thus increasing the size of the vector. Objects at any position in the vector may be removed, thus shrinking the size of the Vector. Objects at any position in the Vector may be replaced, which does not affect the vector’s size.
The capacity of a vector may be specified when the vector is created. If the capacity of the vector is exceeded, the capacity is increased (doubled by default).
See also
Fields
protected int elementCount | The number of elements or the size of the vector. |
protected Object[] elementData | The elements of the vector. |
protected int capacityIncrement | How many elements should be added to the vector when it is detected that it needs to grow to accommodate extra entries. |
Constructors
public Vector() | Constructs a new vector using the default capacity. |
public Vector(int capacity) | Constructs a new vector using the specified capacity. |
public Vector(int capacity, int capacityIncrement) | Constructs a new vector using the specified capacity and capacity increment. |
public Vector(Collection<? extends E> collection) | Constructs a new instance of Vector containing the elements in collection. |
Methods
public void add(int location, E object) | Adds the specified object into this vector at the specified location. |
public synchronized boolean add(E object) | Adds the specified object at the end of this vector. |
public synchronized boolean addAll(int location, Collection<? extends E> collection) | Inserts the objects in the specified collection at the specified location in this vector. |
public synchronized boolean addAll(Collection<? extends E> collection) | Adds the objects in the specified collection to the end of this vector. |
public synchronized void addElement(E object) | Adds the specified object at the end of this vector. |
public synchronized int capacity() | Returns the number of elements this vector can hold without growing. |
public void clear() | Removes all elements from this vector, leaving it empty. |
public boolean contains(Object object) | Searches this vector for the specified object. |
public synchronized boolean containsAll(Collection<?> collection) | Searches this vector for all objects in the specified collection. |
public synchronized void copyInto(Object[] elements) | Attempts to copy elements contained by this Vector into the corresponding elements of the supplied Object array. |
public synchronized E elementAt(int location) | Returns the element at the specified location in this vector. |
public Enumeration<E> elements() | Returns an enumeration on the elements of this vector. |
public synchronized void ensureCapacity(int minimumCapacity) | Ensures that this vector can hold the specified number of elements without growing. |
public synchronized boolean equals(Object object) | Compares the specified object to this vector and returns if they are equal. |
public synchronized E firstElement() | Returns the first element in this vector. |
public E get(int location) | Returns the element at the specified location in this vector. |
public synchronized int hashCode() | Returns an integer hash code for the receiver. |
public int indexOf(Object object) | Searches in this vector for the index of the specified object. |
public synchronized int indexOf(Object object, int location) | Searches in this vector for the index of the specified object. |
public synchronized void insertElementAt(E object, int location) | Inserts the specified object into this vector at the specified location. |
public synchronized boolean isEmpty() | Returns if this vector has no elements, a size of zero. |
public synchronized E lastElement() | Returns the last element in this vector. |
public synchronized int lastIndexOf(Object object) | Searches in this vector for the index of the specified object. |
public synchronized int lastIndexOf(Object object, int location) | Searches in this vector for the index of the specified object. |
public synchronized E remove(int location) | Removes the object at the specified location from this vector. |
public boolean remove(Object object) | Removes the first occurrence, starting at the beginning and moving towards the end, of the specified object from this vector. |
public synchronized boolean removeAll(Collection<?> collection) | Removes all occurrences in this vector of each object in the specified Collection. |
public synchronized void removeAllElements() | Removes all elements from this vector, leaving the size zero and the capacity unchanged. |
public synchronized boolean removeElement(Object object) | Removes the first occurrence, starting at the beginning and moving towards the end, of the specified object from this vector. |
public synchronized void removeElementAt(int location) | Removes the element found at index position location from this Vector. |
protected void removeRange(int start, int end) | Removes the objects in the specified range from the start to the, but not including, end index. |
public synchronized boolean retainAll(Collection<?> collection) | Removes all objects from this vector that are not contained in the specified collection. |
public synchronized E set(int location, E object) | Replaces the element at the specified location in this vector with the specified object. |
public synchronized void setElementAt(E object, int location) | Replaces the element at the specified location in this vector with the specified object. |
public synchronized void setSize(int length) | Sets the size of this vector to the specified size. |
public synchronized int size() | Returns the number of elements in this vector. |
public synchronized List<E> subList(int start, int end) | Returns a List of the specified portion of this vector from the start index to one less than the end index. |
public synchronized Object[] toArray() | Returns a new array containing all elements contained in this vector. |
public synchronized <T> T[] toArray(T[] contents) | Returns an array containing all elements contained in this vector. |
public synchronized String toString() | Returns the string representation of this vector. |
public synchronized void trimToSize() | Sets the capacity of this vector to be the same as the size. |
Inherited fields
From AbstractList
Inherited methods
From Collection
From List
Field details
elementCount
protected int elementCountThe number of elements or the size of the vector.
elementData
protected Object[] elementDataThe elements of the vector.
capacityIncrement
protected int capacityIncrementHow many elements should be added to the vector when it is detected that
it needs to grow to accommodate extra entries. If this value is zero or
negative the size will be doubled if an increase is needed.
Constructor details
Vector
public Vector()Constructs a new vector using the default capacity.
Vector
public Vector(int capacity)Constructs a new vector using the specified capacity.
Parameters
capacityint- the initial capacity of the new vector.
Throws
IllegalArgumentException- if
capacityis negative.
Vector
public Vector(int capacity, int capacityIncrement)Constructs a new vector using the specified capacity and capacity
increment.
Parameters
capacityint- the initial capacity of the new vector.
capacityIncrementint- the amount to increase the capacity when this vector is full.
Throws
IllegalArgumentException- if
capacityis negative.
Vector
public Vector(Collection<? extends E> collection)Constructs a new instance of
Vector containing the elements in
collection. The order of the elements in the new Vector
is dependent on the iteration order of the seed collection.Parameters
collectionCollection<? extends E>- the collection of elements to add.
Method details
add
public void add(int location, E object)Adds the specified object into this vector at the specified location. The
object is inserted before any element with the same or a higher index
increasing their index by 1. If the location is equal to the size of this
vector, the object is added at the end.
Parameters
locationint- the index at which to insert the element.
objectE- the object to insert in this vector.
Throws
ArrayIndexOutOfBoundsException- if
location size(). UnsupportedOperationException- if adding to this List is not supported.
ClassCastException- if the class of the object is inappropriate for this List
IllegalArgumentException- if the object cannot be added to this List
IndexOutOfBoundsException- if
location = size()
See also
add
public synchronized boolean add(E object)Adds the specified object at the end of this vector.
Parameters
objectE- the object to add to the vector.
Returns
trueaddAll
public synchronized boolean addAll(int location, Collection<? extends E> collection)Inserts the objects in the specified collection at the specified location
in this vector. The objects are inserted in the order in which they are
returned from the Collection iterator. The elements with an index equal
or higher than
location have their index increased by the size of
the added collection.Parameters
locationint- the location to insert the objects.
collectionCollection<? extends E>- the collection of objects.
Returns
true if this vector is modified, false otherwise.Throws
ArrayIndexOutOfBoundsException- if
location size().
addAll
public synchronized boolean addAll(Collection<? extends E> collection)Adds the objects in the specified collection to the end of this vector.
Parameters
collectionCollection<? extends E>- the collection of objects.
Returns
true if this vector is modified, false otherwise.addElement
public synchronized void addElement(E object)Adds the specified object at the end of this vector.
Parameters
objectE- the object to add to the vector.
capacity
public synchronized int capacity()Returns the number of elements this vector can hold without growing.
Returns
the capacity of this vector.
See also
clear
public void clear()Removes all elements from this vector, leaving it empty.
Throws
UnsupportedOperationException- if removing from this list is not supported.
contains
public boolean contains(Object object)Searches this vector for the specified object.
Parameters
objectObject- the object to look for in this vector.
Returns
true if object is an element of this vector,
false otherwise.containsAll
public synchronized boolean containsAll(Collection<?> collection)Searches this vector for all objects in the specified collection.
Parameters
collectionCollection<?>- the collection of objects.
Returns
true if all objects in the specified collection are
elements of this vector, false otherwise.copyInto
public synchronized void copyInto(Object[] elements)Attempts to copy elements contained by this
Vector into the
corresponding elements of the supplied Object array.Parameters
elementsObject[]- the
Objectarray into which the elements of this vector are copied.
Throws
IndexOutOfBoundsException- if
elementsis not big enough.
See also
elementAt
public synchronized E elementAt(int location)Returns the element at the specified location in this vector.
Parameters
locationint- the index of the element to return in this vector.
Returns
the element at the specified location.
Throws
ArrayIndexOutOfBoundsException- if
location = size().
See also
elements
public Enumeration<E> elements()Returns an enumeration on the elements of this vector. The results of the
enumeration may be affected if the contents of this vector is modified.
Returns
an enumeration of the elements of this vector.
See also
ensureCapacity
public synchronized void ensureCapacity(int minimumCapacity)Ensures that this vector can hold the specified number of elements
without growing.
Parameters
minimumCapacityint- the minimum number of elements that this vector will hold before growing.
See also
equals
public synchronized boolean equals(Object object)Compares the specified object to this vector and returns if they are
equal. The object must be a List which contains the same objects in the
same order.
Parameters
objectObject- the object to compare with this object
Returns
true if the specified object is equal to this vector,
false otherwise.See also
firstElement
public synchronized E firstElement()Returns the first element in this vector.
Returns
the element at the first position.
Throws
NoSuchElementException- if this vector is empty.
See also
get
public E get(int location)Returns the element at the specified location in this vector.
Parameters
locationint- the index of the element to return in this vector.
Returns
the element at the specified location.
Throws
ArrayIndexOutOfBoundsException- if
location = size().
See also
hashCode
public synchronized int hashCode()Returns an integer hash code for the receiver. Objects which are equal
return the same value for this method.
Returns
the receiver’s hash.
See also
indexOf
public int indexOf(Object object)Searches in this vector for the index of the specified object. The search
for the object starts at the beginning and moves towards the end of this
vector.
Parameters
objectObject- the object to find in this vector.
Returns
the index in this vector of the specified element, -1 if the
element isn’t found.
indexOf
public synchronized int indexOf(Object object, int location)Searches in this vector for the index of the specified object. The search
for the object starts at the specified location and moves towards the end
of this vector.
Parameters
objectObject- the object to find in this vector.
locationint- the index at which to start searching.
Returns
the index in this vector of the specified element, -1 if the
element isn’t found.
Throws
ArrayIndexOutOfBoundsException- if
location < 0.
insertElementAt
public synchronized void insertElementAt(E object, int location)Inserts the specified object into this vector at the specified location.
This object is inserted before any previous element at the specified
location. All elements with an index equal or greater than
location have their index increased by 1. If the location is
equal to the size of this vector, the object is added at the end.Parameters
objectE- the object to insert in this vector.
locationint- the index at which to insert the element.
Throws
ArrayIndexOutOfBoundsException- if
location size().
See also
isEmpty
public synchronized boolean isEmpty()Returns if this vector has no elements, a size of zero.
Returns
true if this vector has no elements, false
otherwise.See also
lastElement
public synchronized E lastElement()Returns the last element in this vector.
Returns
the element at the last position.
Throws
NoSuchElementException- if this vector is empty.
See also
lastIndexOf
public synchronized int lastIndexOf(Object object)Searches in this vector for the index of the specified object. The search
for the object starts at the end and moves towards the start of this
vector.
Parameters
objectObject- the object to find in this vector.
Returns
the index in this vector of the specified element, -1 if the
element isn’t found.
lastIndexOf
public synchronized int lastIndexOf(Object object, int location)Searches in this vector for the index of the specified object. The search
for the object starts at the specified location and moves towards the
start of this vector.
Parameters
objectObject- the object to find in this vector.
locationint- the index at which to start searching.
Returns
the index in this vector of the specified element, -1 if the
element isn’t found.
Throws
ArrayIndexOutOfBoundsException- if
location >= size().
remove
public synchronized E remove(int location)Removes the object at the specified location from this vector. All
elements with an index bigger than
location have their index
decreased by 1.Parameters
locationint- the index of the object to remove.
Returns
the removed object.
Throws
IndexOutOfBoundsException- if
location = size().
remove
public boolean remove(Object object)Removes the first occurrence, starting at the beginning and moving
towards the end, of the specified object from this vector. All elements
with an index bigger than the element that gets removed have their index
decreased by 1.
Parameters
objectObject- the object to remove from this vector.
Returns
true if the specified object was found, false
otherwise.removeAll
public synchronized boolean removeAll(Collection<?> collection)Removes all occurrences in this vector of each object in the specified
Collection.
Parameters
collectionCollection<?>- the collection of objects to remove.
Returns
true if this vector is modified, false otherwise.See also
removeAllElements
public synchronized void removeAllElements()Removes all elements from this vector, leaving the size zero and the
capacity unchanged.
removeElement
public synchronized boolean removeElement(Object object)Removes the first occurrence, starting at the beginning and moving
towards the end, of the specified object from this vector. All elements
with an index bigger than the element that gets removed have their index
decreased by 1.
Parameters
objectObject- the object to remove from this vector.
Returns
true if the specified object was found, false
otherwise.removeElementAt
public synchronized void removeElementAt(int location)Removes the element found at index position
location from
this Vector. All elements with an index bigger than
location have their index decreased by 1.Parameters
locationint- the index of the element to remove.
Throws
ArrayIndexOutOfBoundsException- if
location = size().
removeRange
protected void removeRange(int start, int end)Removes the objects in the specified range from the start to the, but not
including, end index. All elements with an index bigger than or equal to
end have their index decreased by end - start.Parameters
startint- the index at which to start removing.
endint- the index one past the end of the range to remove.
Throws
IndexOutOfBoundsException- if
start endorend > size(). UnsupportedOperationException- if removing from this list is not supported.
retainAll
public synchronized boolean retainAll(Collection<?> collection)Removes all objects from this vector that are not contained in the
specified collection.
Parameters
collectionCollection<?>- the collection of objects to retain.
Returns
true if this vector is modified, false otherwise.See also
set
public synchronized E set(int location, E object)Replaces the element at the specified location in this vector with the
specified object.
Parameters
locationint- the index at which to put the specified object.
objectE- the object to add to this vector.
Returns
the previous element at the location.
Throws
ArrayIndexOutOfBoundsException- if
location = size().
See also
setElementAt
public synchronized void setElementAt(E object, int location)Replaces the element at the specified location in this vector with the
specified object.
Parameters
objectE- the object to add to this vector.
locationint- the index at which to put the specified object.
Throws
ArrayIndexOutOfBoundsException- if
location = size().
See also
setSize
public synchronized void setSize(int length)Sets the size of this vector to the specified size. If there are more
than length elements in this vector, the elements at end are lost. If
there are less than length elements in the vector, the additional
elements contain null.
Parameters
lengthint- the new size of this vector.
See also
size
public synchronized int size()Returns the number of elements in this vector.
Returns
the number of elements in this vector.
See also
subList
public synchronized List<E> subList(int start, int end)Returns a List of the specified portion of this vector from the start
index to one less than the end index. The returned List is backed by this
vector so changes to one are reflected by the other.
Parameters
startint- the index at which to start the sublist.
endint- the index one past the end of the sublist.
Returns
a List of a portion of this vector.
Throws
IndexOutOfBoundsException- if
start size(). IllegalArgumentException- if
start > end.
toArray
public synchronized Object[] toArray()Returns a new array containing all elements contained in this vector.
Returns
an array of the elements from this vector.
toArray
public synchronized <T> T[] toArray(T[] contents)Returns an array containing all elements contained in this vector. If the
specified array is large enough to hold the elements, the specified array
is used, otherwise an array of the same type is created. If the specified
array is used and is larger than this vector, the array element following
the collection elements is set to null.
Parameters
contentsT[]- the array to fill.
Returns
an array of the elements from this vector.
Throws
ArrayStoreException- if the type of an element in this vector cannot be stored in the type of the specified array.
toString
public synchronized String toString()Returns the string representation of this vector.
Returns
the string representation of this vector.
See also
trimToSize
public synchronized void trimToSize()Sets the capacity of this vector to be the same as the size.