Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## java.util
Class Vector<E>

    java.lang.Object
      java.util.Vector<E>

All Implemented Interfaces:
  Iterable<E>

Direct Known Subclasses:
  Stack

    @TransactionType(value=SUPPORTS)
    public class Vector<E>

extends Object
implements Iterable<E>

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 Vector's iterator method are fail-fast: if the Vector is structurally modified at any time after the Iterator is created, the Iterator will throw a RuntimeException. 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 Vector's elements method are not fail-fast.

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 `RuntimeException` 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.

This class is thread-safe (synchronzied).

Since:
  JDK1.0, CLDC 1.0, Java Card 3.0

Field Summary
`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 the vector.
`protected Object[]` | `elementData` The array buffer into which the components of the vector are stored.
`protected int` | `modCount` The number of times this vector has been structurally modified.

Constructor Summary
`Vector()` Constructs an empty vector so that its internal data array has size `10` and its standard capacity increment is zero.
`Vector(int initialCapacity)` Constructs an empty vector with the specified initial capacity and with its capacity increment equal to zero.
`Vector(int initialCapacity, int capacityIncrement)` Constructs an empty vector with the specified initial capacity and capacity increment.

Method Summary
`void` | `addElement(E obj)` Adds the specified component to the end of this vector, increasing its size by one.
`int` | `capacity()` Returns the current capacity of this vector.
`boolean` | `contains(Object elem)` Tests if the specified object is a component in this vector.
`void` | `copyInto(Object[] anArray)` Copies the components of this vector into the specified array.
`E` | `elementAt(int index)` Returns the component at the specified index.
`Enumeration<E>` | `elements()` Returns an enumeration of the components of this vector.
`void` | `ensureCapacity(int 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.
`boolean` | `equals(Object o)` Compares the specified object with this vector for equality.
`E` | `firstElement()` Returns the first component of this vector.
`int` | `hashCode()` Returns the hash code value for this vector.
`int` | `indexOf(Object elem)` Searches for the first occurence of the given argument, testing for equality using the `equals` method.
`int` | `indexOf(Object elem, int index)` Searches for the first occurence of the given argument, beginning the search at `index`, and testing for equality using the `equals` method.
`void` | `insertElementAt(E obj, int index)` Inserts the specified object as a component in this vector at the specified `index`.
`boolean` | `isEmpty()` Tests if this vector has no components.
`Iterator<E>` | `iterator()` Returns an iterator over a set of elements of type T.
`E` | `lastElement()` Returns the last component of the vector.
`int` | `lastIndexOf(Object elem)` Returns the index of the last occurrence of the specified object in this vector.
`int` | `lastIndexOf(Object elem, int index)` Searches backwards for the specified object, starting from the specified index, and returns an index to it.
`void` | `removeAllElements()` Removes all components from this vector and sets its size to zero.
`boolean` | `removeElement(Object obj)` Removes the first occurrence of the argument from this vector.
`void` | `removeElementAt(int index)` Deletes the component at the specified index.
`void` | `setElementAt(E obj, int index)` Sets the component at the specified `index` of this vector to be the specified object.
`void` | `setSize(int newSize)` Sets the size of this vector.
`int` | `size()` Returns the number of components in this vector.
`String` | `toString()` Returns a string representation of this vector.
`void` | `trimToSize()` Trims the capacity of this vector to be the vector's current size.

Methods inherited from class java.lang.Object
`getClass, notify, notifyAll, wait, wait, wait`

Field Detail

### elementData

    protected Object[] elementData

  The array buffer into which the components of the vector are stored. The capacity of the vector is the length of this array buffer.

  Since:
    JDK1.0

### elementCount

    protected int elementCount

  The number of valid components in the vector.

  Since:
    JDK1.0

### capacityIncrement

    protected int capacityIncrement

  The amount by which the capacity of the vector is automatically incremented when its size becomes greater than its capacity. If the capacity increment is `0`, the capacity of the vector is doubled each time it needs to grow.

  Since:
    JDK1.0

### modCount

    protected int modCount

  The number of times this vector has been structurally modified. Structural modifications are those that change the size of the vector, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.

  This field is used by the iterator implementation returned by the `iterator` method. If the value of this field changes unexpectedly, the iterator will throw a `RuntimeException` in response to the `next` operation. This provides fail-fast behavior, rather than non-deterministic behavior in the face of concurrent modification during iteration.

  Use of this field by subclasses is optional. If a subclass wishes to provide fail-fast iterators, then it merely has to increment this field in methods that it overrides that result in structural modifications to the vector. If an implementation does not wish to provide fail-fast iterators, this field may be ignored.

Constructor Detail

### Vector

    public Vector(int initialCapacity,
                  int capacityIncrement)

  Constructs an empty vector with the specified initial capacity and capacity increment.

  Parameters:
    `initialCapacity` - the initial capacity of the vector.
    `capacityIncrement` - the amount by which the capacity is increased when the vector overflows.
  Throws:
    `IllegalArgumentException` - if the specified initial capacity is negative

### Vector

    public Vector(int initialCapacity)

  Constructs an empty vector with the specified initial capacity and with its capacity increment equal to zero.

  Parameters:
    `initialCapacity` - the initial capacity of the vector.
  Since:
    JDK1.0

### Vector

    public Vector()

  Constructs an empty vector so that its internal data array has size `10` and its standard capacity increment is zero.

  Since:
    JDK1.0

Method Detail

### copyInto

    public void copyInto(Object[] anArray)

  Copies the components of this vector into the specified array. The array must be big enough to hold all the objects in this vector.

  Parameters:
    `anArray` - the array into which the components get copied.
  Since:
    JDK1.0

### trimToSize

    public void trimToSize()

  Trims the capacity of this vector to be the vector's current size. An application can use this operation to minimize the storage of a vector.

  Since:
    JDK1.0

### ensureCapacity

    public void ensureCapacity(int 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.

  Parameters:
    `minCapacity` - the desired minimum capacity.
  Since:
    JDK1.0

### setSize

    public void setSize(int newSize)

  Sets the size of this vector. If the new size is greater than the current size, new `null` items are added to the end of the vector. If the new size is less than the current size, all components at index `newSize` and greater are discarded.

  Parameters:
    `newSize` - the new size of this vector.
  Throws:
    `ArrayIndexOutOfBoundsException` - if new size is negative.
  Since:
    JDK1.0

### capacity

    public int capacity()

  Returns the current capacity of this vector.

  Returns:
    the current capacity of this vector.
  Since:
    JDK1.0

### size

    public int size()

  Returns the number of components in this vector.

  Returns:
    the number of components in this vector.
  Since:
    JDK1.0

### isEmpty

    public boolean isEmpty()

  Tests if this vector has no components.

  Returns:
    `true` if this vector has no components; `false` otherwise.
  Since:
    JDK1.0

### elements

    public Enumeration<E> elements()

  Returns an enumeration of the components of this vector.

  Returns:
    an enumeration of the components of this vector.
  Since:
    JDK1.0
  See Also:
    `Enumeration`

### contains

    public boolean contains(Object elem)

  Tests if the specified object is a component in this vector.

  Parameters:
    `elem` - an object.
  Returns:
    `true` if the specified object is a component in this vector; `false` otherwise.
  Since:
    JDK1.0

### indexOf

    public int indexOf(Object elem)

  Searches for the first occurence of the given argument, testing for equality using the `equals` method.

  Parameters:
    `elem` - an object.
  Returns:
    the index of the first occurrence of the argument in this vector; returns `-1` if the object is not found.
  Since:
    JDK1.0
  See Also:
    `Object.equals(java.lang.Object)`

### indexOf

    public int indexOf(Object elem,
                       int index)

  Searches for the first occurence of the given argument, beginning the search at `index`, and testing for equality using the `equals` method.

  Parameters:
    `elem` - an object.
    `index` - the index to start searching from.
  Returns:
    the index of the first occurrence of the object argument in this vector at position `index` or later in the vector; returns `-1` if the object is not found.
  Since:
    JDK1.0
  See Also:
    `Object.equals(java.lang.Object)`

### lastIndexOf

    public int lastIndexOf(Object elem)

  Returns the index of the last occurrence of the specified object in this vector.

  Parameters:
    `elem` - the desired component.
  Returns:
    the index of the last occurrence of the specified object in this vector; returns `-1` if the object is not found.
  Since:
    JDK1.0

### lastIndexOf

    public int lastIndexOf(Object elem,
                           int index)

  Searches backwards for the specified object, starting from the specified index, and returns an index to it.

  Parameters:
    `elem` - the desired component.
    `index` - the index to start searching from.
  Returns:
    the index of the last occurrence of the specified object in this vector at position less than `index` in the vector; `-1` if the object is not found.
  Throws:
    `IndexOutOfBoundsException` - if `index` is greater than or equal to the current size of this vector.
  Since:
    JDK1.0

### elementAt

    public E elementAt(int index)

  Returns the component at the specified index.

  Parameters:
    `index` - an index into this vector.
  Returns:
    the component at the specified index.
  Throws:
    `ArrayIndexOutOfBoundsException` - if an invalid index was given.
  Since:
    JDK1.0

### firstElement

    public E firstElement()

  Returns the first component of this vector.

  Returns:
    the first component of this vector.
  Throws:
    `NoSuchElementException` - if this vector has no components.
  Since:
    JDK1.0

### lastElement

    public E lastElement()

  Returns the last component of the vector.

  Returns:
    the last component of the vector, i.e., the component at index `size() - 1`.
  Throws:
    `NoSuchElementException` - if this vector is empty.
  Since:
    JDK1.0

### setElementAt

    public void setElementAt(E obj,
                             int index)

  Sets the component at the specified `index` of this vector to be the specified object. The previous component at that position is discarded.

  The index must be a value greater than or equal to `0` and less than the current size of the vector.

  Parameters:
    `obj` - what the component is to be set to.
    `index` - the specified index.
  Throws:
    `ArrayIndexOutOfBoundsException` - if the index was invalid.
  Since:
    JDK1.0
  See Also:
    `size()`

### removeElementAt

    public void removeElementAt(int index)

  Deletes the component at the specified index. Each component in this vector with an index greater or equal to the specified `index` is shifted downward to have an index one smaller than the value it had previously.

  The index must be a value greater than or equal to `0` and less than the current size of the vector.

  Parameters:
    `index` - the index of the object to remove.
  Throws:
    `ArrayIndexOutOfBoundsException` - if the index was invalid.
  Since:
    JDK1.0
  See Also:
    `size()`

### insertElementAt

    public void insertElementAt(E obj,
                                int index)

  Inserts the specified object as a component in this vector at the specified `index`. Each component in this vector with an index greater or equal to the specified `index` is shifted upward to have an index one greater than the value it had previously.

  The index must be a value greater than or equal to `0` and less than or equal to the current size of the vector.

  Parameters:
    `obj` - the component to insert.
    `index` - where to insert the new component.
  Throws:
    `ArrayIndexOutOfBoundsException` - if the index was invalid.
  Since:
    JDK1.0
  See Also:
    `size()`

### addElement

    public void addElement(E obj)

  Adds the specified component to the end of this vector, increasing its size by one. The capacity of this vector is increased if its size becomes greater than its capacity.

  Parameters:
    `obj` - the component to be added.
  Since:
    JDK1.0

### removeElement

    public boolean removeElement(Object obj)

  Removes the first occurrence of the argument from this vector. If the object is found in this vector, each component in the vector with an index greater or equal to the object's index is shifted downward to have an index one smaller than the value it had previously.

  Parameters:
    `obj` - the component to be removed.
  Returns:
    `true` if the argument was a component of this vector; `false` otherwise.
  Since:
    JDK1.0

### removeAllElements

    public void removeAllElements()

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

  Since:
    JDK1.0

### toString

    public String toString()

  Returns a string representation of this vector.

  Overrides:
    `toString` in class `Object`
  Returns:
    a string representation of this vector.
  Since:
    JDK1.0

### iterator

    public Iterator<E> iterator()

  Description copied from interface: `Iterable`
  Returns an iterator over a set of elements of type T.

  Specified by:
    `iterator` in interface `Iterable<E>`
  Returns:
    an Iterator.

### equals

    public boolean equals(Object o)

  Compares the specified object with this vector for equality. Returns true if and only if the specified object is also a vector, both vectors have the same size, and all corresponding pairs of elements in the two vectors are equal. (Two elements `e1` and `e2` are equal if `(e1==null ? e2==null : e1.equals(e2))`.) In other words, two vectors are defined to be equal if they contain the same elements in the same order.

  Overrides:
    `equals` in class `Object`
  Parameters:
    `o` - the object to be compared for equality with this vector
  Returns:
    true if the specified object is equal to this vector
  See Also:
    `Boolean.hashCode()`, `Hashtable`

### hashCode

    public int hashCode()

  Returns the hash code value for this vector. The hash code of a vector is defined to be the result of the following calculation:

        int hashCode = 1;
        Iterator<E> i = list.iterator();
        while (i.hasNext()) {
            E obj = i.next();
            hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
        }

  This ensures that `vector1.equals(vector2)` implies that `vector1.hashCode()==vector2.hashCode()` for any two vectors, `vector1` and `vector2`, as required by the general contract of `Object.hashCode()`.

  Overrides:
    `hashCode` in class `Object`
  Returns:
    the hash code value for this Vector
  See Also:
    `Object.equals(java.lang.Object)`, `Hashtable`

Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright (c) 2009 Sun Microsystems, Inc. All rights reserved.