Intel

Developer Zone

Vector

java.util

Class Vector<E>

  • Direct Known Subclasses:
    Stack


    public class Vector<E>
    extends Object
    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:
    StringBuffer
    • Constructor Summary

      Constructors
      Constructor and 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.
    • Method Summary

      Methods
      Modifier and Type Method and Description
      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.
      boolean contains(Object object)
      Searches this vector for the specified object.
      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.
      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.
      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()
      String toString()
      Returns the string representation of this vector.
      void trimToSize()
      Sets the capacity of this vector to be the same as the size.
    • Constructor Detail

      • 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.
    • Method Detail

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

        public int size()
      • 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()
      • get

        public E get(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()
      • 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:
        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.
        Returns:
        true if this vector has no elements, false otherwise.
        See Also:
        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.
        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)
      • 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()
      • 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()
      • toString

        public String toString()
        Returns the string representation of this vector.
        Overrides:
        toString in class Object
        Returns:
        the string representation of this vector.
        See Also:
        elements()
      • 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.
        Overrides:
        equals in class Object
        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()
      • hashCode

        public int hashCode()
        Returns an integer hash code for the receiver. Objects which are equal return the same value for this method.
        Overrides:
        hashCode in class Object
        Returns:
        the receiver's hash.
        See Also:
        equals(java.lang.Object)