Package java.util

Class Vector<E>

Type Parameters:
E - The element type of this list.
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess
Direct Known Subclasses:
Stack

public class Vector<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, Serializable
Vector is an implementation of List, backed by an array and synchronized. All optional operations including adding, removing, and replacing elements are supported.

All elements are permitted, including null.

This class is equivalent to ArrayList with synchronized operations. This has a performance cost, and the synchronization is not necessarily meaningful to your application: synchronizing each call to get, for example, is not equivalent to synchronizing on the list and iterating over it (which is probably what you intended). If you do need very highly concurrent access, you should also consider CopyOnWriteArrayList.

See Also:
Serialized Form
  • Field Summary

    Fields
    Modifier and Type Field Description
    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.
    protected int elementCount
    The number of elements or the size of the vector.
    protected Object[] elementData
    The elements of the vector.

    Fields inherited from class java.util.AbstractList

    modCount
  • Constructor Summary

    Constructors
    Constructor Description
    Vector()
    Constructs a new vector using the default capacity.
    Vector​(int capacity)
    Constructs a new vector using the specified capacity.
    Vector​(int capacity, int capacityIncrement)
    Constructs a new vector using the specified capacity and capacity increment.
    Vector​(Collection<? extends E> collection)
    Constructs a new instance of Vector containing the elements in collection.
  • Method Summary

    Modifier and Type Method Description
    void add​(int location, E object)
    Adds the specified object into this vector at the specified location.
    boolean add​(E object)
    Adds the specified object at the end of this vector.
    boolean addAll​(int location, Collection<? extends E> collection)
    Inserts the objects in the specified collection at the specified location in this vector.
    boolean addAll​(Collection<? extends E> collection)
    Adds the objects in the specified collection to the end of this vector.
    void addElement​(E object)
    Adds the specified object at the end of this vector.
    int capacity()
    Returns the number of elements this vector can hold without growing.
    void clear()
    Removes all elements from this vector, leaving it empty.
    Object clone()
    Returns a new vector with the same elements, size, capacity and capacity increment as this vector.
    boolean contains​(Object object)
    Searches this vector for the specified object.
    boolean containsAll​(Collection<?> collection)
    Searches this vector for all objects in the specified collection.
    void copyInto​(Object[] elements)
    Attempts to copy elements contained by this Vector into the corresponding elements of the supplied Object array.
    E elementAt​(int location)
    Returns the element at the specified location in this vector.
    Enumeration<E> elements()
    Returns an enumeration on the elements of this vector.
    void ensureCapacity​(int minimumCapacity)
    Ensures that this vector can hold the specified number of elements without growing.
    boolean equals​(Object object)
    Compares the specified object to this vector and returns if they are equal.
    E firstElement()
    Returns the first element in this vector.
    E get​(int location)
    Returns the element at the specified location in this vector.
    int hashCode()
    Returns an integer hash code for the receiver.
    int indexOf​(Object object)
    Searches in this vector for the index of the specified object.
    int indexOf​(Object object, int location)
    Searches in this vector for the index of the specified object.
    void insertElementAt​(E object, int location)
    Inserts the specified object into this vector at the specified location.
    boolean isEmpty()
    Returns if this vector has no elements, a size of zero.
    E lastElement()
    Returns the last element in this vector.
    int lastIndexOf​(Object object)
    Searches in this vector for the index of the specified object.
    int lastIndexOf​(Object object, int location)
    Searches in this vector for the index of the specified object.
    E remove​(int location)
    Removes the object at the specified location from this vector.
    boolean remove​(Object object)
    Removes the first occurrence, starting at the beginning and moving towards the end, of the specified object from this vector.
    boolean removeAll​(Collection<?> collection)
    Removes all occurrences in this vector of each object in the specified Collection.
    void removeAllElements()
    Removes all elements from this vector, leaving the size zero and the capacity unchanged.
    boolean removeElement​(Object object)
    Removes the first occurrence, starting at the beginning and moving towards the end, of the specified object from this vector.
    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.
    boolean retainAll​(Collection<?> collection)
    Removes all objects from this vector that are not contained in the specified collection.
    E set​(int location, E object)
    Replaces the element at the specified location in this vector with the specified object.
    void setElementAt​(E object, int location)
    Replaces the element at the specified location in this vector with the specified object.
    void setSize​(int length)
    Sets the size of this vector to the specified size.
    int size()
    Returns the number of elements in this vector.
    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.
    Object[] toArray()
    Returns a new array containing all elements contained in this vector.
    <T> T[] toArray​(T[] contents)
    Returns an array containing all elements contained in this vector.
    String toString()
    Returns the string representation of this vector.
    void trimToSize()
    Sets the capacity of this vector to be the same as the size.

    Methods inherited from class java.util.AbstractList

    iterator, listIterator, listIterator

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.util.List

    iterator, listIterator, listIterator
  • Field Details

    • elementCount

      protected int elementCount
      The number of elements or the size of the vector.
    • elementData

      protected Object[] elementData
      The elements of the vector.
    • capacityIncrement

      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. 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:
      capacity - the initial capacity of the new vector.
      Throws:
      IllegalArgumentException - if capacity is negative.
    • Vector

      public Vector​(int capacity, int capacityIncrement)
      Constructs a new vector using the specified capacity and capacity increment.
      Parameters:
      capacity - the initial capacity of the new vector.
      capacityIncrement - the amount to increase the capacity when this vector is full.
      Throws:
      IllegalArgumentException - if capacity is 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:
      collection - 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.
      Specified by:
      add in interface List<E>
      Overrides:
      add in class AbstractList<E>
      Parameters:
      location - the index at which to insert the element.
      object - the object to insert in this vector.
      Throws:
      ArrayIndexOutOfBoundsException - if location < 0 || location > size().
      See Also:
      addElement(E), size()
    • add

      public boolean add​(E object)
      Adds the specified object at the end of this vector.
      Specified by:
      add in interface Collection<E>
      Specified by:
      add in interface List<E>
      Overrides:
      add in class AbstractList<E>
      Parameters:
      object - the object to add to the vector.
      Returns:
      true
    • addAll

      public 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.
      Specified by:
      addAll in interface List<E>
      Overrides:
      addAll in class AbstractList<E>
      Parameters:
      location - the location to insert the objects.
      collection - the collection of objects.
      Returns:
      true if this vector is modified, false otherwise.
      Throws:
      ArrayIndexOutOfBoundsException - if location < 0 or location > size().
    • addAll

      public boolean addAll​(Collection<? extends E> collection)
      Adds the objects in the specified collection to the end of this vector.
      Specified by:
      addAll in interface Collection<E>
      Specified by:
      addAll in interface List<E>
      Overrides:
      addAll in class AbstractCollection<E>
      Parameters:
      collection - the collection of objects.
      Returns:
      true if this vector is modified, false otherwise.
    • addElement

      public void addElement​(E object)
      Adds the specified object at the end of this vector.
      Parameters:
      object - the object to add to the vector.
    • capacity

      public int capacity()
      Returns the number of elements this vector can hold without growing.
      Returns:
      the capacity of this vector.
      See Also:
      ensureCapacity(int), size()
    • clear

      public void clear()
      Removes all elements from this vector, leaving it empty.
      Specified by:
      clear in interface Collection<E>
      Specified by:
      clear in interface List<E>
      Overrides:
      clear in class AbstractList<E>
      See Also:
      isEmpty(), size()
    • clone

      public Object clone()
      Returns a new vector with the same elements, size, capacity and capacity increment as this vector.
      Overrides:
      clone in class Object
      Returns:
      a shallow copy of this vector.
      See Also:
      Cloneable
    • contains

      public boolean contains​(Object object)
      Searches this vector for the specified object.
      Specified by:
      contains in interface Collection<E>
      Specified by:
      contains in interface List<E>
      Overrides:
      contains in class AbstractCollection<E>
      Parameters:
      object - the object to look for in this vector.
      Returns:
      true if object is an element of this vector, false otherwise.
      See Also:
      indexOf(Object), indexOf(Object, int), Object.equals(java.lang.Object)
    • containsAll

      public boolean containsAll​(Collection<?> collection)
      Searches this vector for all objects in the specified collection.
      Specified by:
      containsAll in interface Collection<E>
      Specified by:
      containsAll in interface List<E>
      Overrides:
      containsAll in class AbstractCollection<E>
      Parameters:
      collection - the collection of objects.
      Returns:
      true if all objects in the specified collection are elements of this vector, false otherwise.
    • copyInto

      public void copyInto​(Object[] elements)
      Attempts to copy elements contained by this Vector into the corresponding elements of the supplied Object array.
      Parameters:
      elements - the Object array into which the elements of this vector are copied.
      Throws:
      IndexOutOfBoundsException - if elements is not big enough.
      See Also:
      clone()
    • elementAt

      public E elementAt​(int location)
      Returns the element at the specified location in this vector.
      Parameters:
      location - the index of the element to return in this vector.
      Returns:
      the element at the specified location.
      Throws:
      ArrayIndexOutOfBoundsException - if location < 0 || location >= size().
      See Also:
      size()
    • 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:
      elementAt(int), Enumeration
    • ensureCapacity

      public void ensureCapacity​(int minimumCapacity)
      Ensures that this vector can hold the specified number of elements without growing.
      Parameters:
      minimumCapacity - the minimum number of elements that this vector will hold before growing.
      See Also:
      capacity()
    • equals

      public 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.
      Specified by:
      equals in interface Collection<E>
      Specified by:
      equals in interface List<E>
      Overrides:
      equals in class AbstractList<E>
      Parameters:
      object - the object to compare with this object
      Returns:
      true if the specified object is equal to this vector, false otherwise.
      See Also:
      hashCode()
    • firstElement

      public 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:
      elementAt(int), lastElement(), size()
    • get

      public E get​(int location)
      Returns the element at the specified location in this vector.
      Specified by:
      get in interface List<E>
      Specified by:
      get in class AbstractList<E>
      Parameters:
      location - the index of the element to return in this vector.
      Returns:
      the element at the specified location.
      Throws:
      ArrayIndexOutOfBoundsException - if location < 0 || location >= size().
      See Also:
      size()
    • hashCode

      public int hashCode()
      Returns an integer hash code for the receiver. Objects which are equal return the same value for this method.
      Specified by:
      hashCode in interface Collection<E>
      Specified by:
      hashCode in interface List<E>
      Overrides:
      hashCode in class AbstractList<E>
      Returns:
      the receiver's hash.
      See Also:
      equals(java.lang.Object)
    • 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.
      Specified by:
      indexOf in interface List<E>
      Overrides:
      indexOf in class AbstractList<E>
      Parameters:
      object - the object to find in this vector.
      Returns:
      the index in this vector of the specified element, -1 if the element isn't found.
      See Also:
      contains(java.lang.Object), lastIndexOf(Object), lastIndexOf(Object, int)
    • indexOf

      public 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:
      object - the object to find in this vector.
      location - 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.
      See Also:
      contains(java.lang.Object), lastIndexOf(Object), lastIndexOf(Object, int)
    • insertElementAt

      public 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:
      object - the object to insert in this vector.
      location - the index at which to insert the element.
      Throws:
      ArrayIndexOutOfBoundsException - if location < 0 || location > size().
      See Also:
      addElement(E), size()
    • isEmpty

      public boolean isEmpty()
      Returns if this vector has no elements, a size of zero.
      Specified by:
      isEmpty in interface Collection<E>
      Specified by:
      isEmpty in interface List<E>
      Overrides:
      isEmpty in class AbstractCollection<E>
      Returns:
      true if this vector has no elements, false otherwise.
      See Also:
      size()
    • lastElement

      public 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:
      elementAt(int), firstElement(), size()
    • lastIndexOf

      public 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.
      Specified by:
      lastIndexOf in interface List<E>
      Overrides:
      lastIndexOf in class AbstractList<E>
      Parameters:
      object - the object to find in this vector.
      Returns:
      the index in this vector of the specified element, -1 if the element isn't found.
      See Also:
      contains(java.lang.Object), indexOf(Object), indexOf(Object, int)
    • lastIndexOf

      public 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:
      object - the object to find in this vector.
      location - 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().
      See Also:
      contains(java.lang.Object), indexOf(Object), indexOf(Object, int)
    • remove

      public 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.
      Specified by:
      remove in interface List<E>
      Overrides:
      remove in class AbstractList<E>
      Parameters:
      location - the index of the object to remove.
      Returns:
      the removed object.
      Throws:
      IndexOutOfBoundsException - if location < 0 || 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.
      Specified by:
      remove in interface Collection<E>
      Specified by:
      remove in interface List<E>
      Overrides:
      remove in class AbstractCollection<E>
      Parameters:
      object - the object to remove from this vector.
      Returns:
      true if the specified object was found, false otherwise.
      See Also:
      removeAllElements(), removeElementAt(int), size()
    • removeAll

      public boolean removeAll​(Collection<?> collection)
      Removes all occurrences in this vector of each object in the specified Collection.
      Specified by:
      removeAll in interface Collection<E>
      Specified by:
      removeAll in interface List<E>
      Overrides:
      removeAll in class AbstractCollection<E>
      Parameters:
      collection - the collection of objects to remove.
      Returns:
      true if this vector is modified, false otherwise.
      See Also:
      remove(Object), contains(Object)
    • removeAllElements

      public void removeAllElements()
      Removes all elements from this vector, leaving the size zero and the capacity unchanged.
      See Also:
      isEmpty(), size()
    • removeElement

      public 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:
      object - the object to remove from this vector.
      Returns:
      true if the specified object was found, false otherwise.
      See Also:
      removeAllElements(), removeElementAt(int), size()
    • removeElementAt

      public 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:
      location - the index of the element to remove.
      Throws:
      ArrayIndexOutOfBoundsException - if location < 0 || location >= size().
      See Also:
      removeElement(java.lang.Object), removeAllElements(), 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.
      Overrides:
      removeRange in class AbstractList<E>
      Parameters:
      start - the index at which to start removing.
      end - the index one past the end of the range to remove.
      Throws:
      IndexOutOfBoundsException - if start < 0, start > end or end > size().
    • retainAll

      public boolean retainAll​(Collection<?> collection)
      Removes all objects from this vector that are not contained in the specified collection.
      Specified by:
      retainAll in interface Collection<E>
      Specified by:
      retainAll in interface List<E>
      Overrides:
      retainAll in class AbstractCollection<E>
      Parameters:
      collection - the collection of objects to retain.
      Returns:
      true if this vector is modified, false otherwise.
      See Also:
      remove(Object)
    • set

      public E set​(int location, E object)
      Replaces the element at the specified location in this vector with the specified object.
      Specified by:
      set in interface List<E>
      Overrides:
      set in class AbstractList<E>
      Parameters:
      location - the index at which to put the specified object.
      object - the object to add to this vector.
      Returns:
      the previous element at the location.
      Throws:
      ArrayIndexOutOfBoundsException - if location < 0 || location >= size().
      See Also:
      size()
    • setElementAt

      public void setElementAt​(E object, int location)
      Replaces the element at the specified location in this vector with the specified object.
      Parameters:
      object - the object to add to this vector.
      location - the index at which to put the specified object.
      Throws:
      ArrayIndexOutOfBoundsException - if location < 0 || location >= size().
      See Also:
      size()
    • setSize

      public 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:
      length - the new size of this vector.
      See Also:
      size()
    • size

      public int size()
      Returns the number of elements in this vector.
      Specified by:
      size in interface Collection<E>
      Specified by:
      size in interface List<E>
      Specified by:
      size in class AbstractCollection<E>
      Returns:
      the number of elements in this vector.
      See Also:
      elementCount, lastElement()
    • subList

      public 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.
      Specified by:
      subList in interface List<E>
      Overrides:
      subList in class AbstractList<E>
      Parameters:
      start - the index at which to start the sublist.
      end - the index one past the end of the sublist.
      Returns:
      a List of a portion of this vector.
      Throws:
      IndexOutOfBoundsException - if start < 0 or end > size().
      IllegalArgumentException - if start > end.
    • toArray

      public Object[] toArray()
      Returns a new array containing all elements contained in this vector.
      Specified by:
      toArray in interface Collection<E>
      Specified by:
      toArray in interface List<E>
      Overrides:
      toArray in class AbstractCollection<E>
      Returns:
      an array of the elements from this vector.
    • toArray

      public <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.
      Specified by:
      toArray in interface Collection<E>
      Specified by:
      toArray in interface List<E>
      Overrides:
      toArray in class AbstractCollection<E>
      Parameters:
      contents - 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 String toString()
      Returns the string representation of this vector.
      Overrides:
      toString in class AbstractCollection<E>
      Returns:
      the string representation of this vector.
      See Also:
      elements()
    • trimToSize

      public void trimToSize()
      Sets the capacity of this vector to be the same as the size.
      See Also:
      capacity(), ensureCapacity(int), size()