Package java.util |
![]() Previous |
![]() Java API |
![]() Index |
![]() Next |
public class java.util.Vector extends java.lang.Object implements java.lang.Cloneable { // Fields protected int capacityIncrement; protected int elementCount; protected Object elementData[]; // Constructors public Vector(); public Vector(int initialCapacity); public Vector(int initialCapacity, int capacityIncrement); // Methods public final void addElement(Object obj); public final int capacity(); public Object clone(); public final boolean contains(Object elem); public final void copyInto(Object anArray[]); public final Object elementAt(int index); public final Enumeration elements(); public final void ensureCapacity(int minCapacity) public final Object firstElement(); public final int indexOf(Object elem); public final int indexOf(Object elem, int index); public final void insertElementAt(Object obj, int index); public final boolean isEmpty(); public final Object lastElement(); public final int lastIndexOf(Object elem); public final int lastIndexOf(Object elem, int index); public final void removeAllElements(); public final boolean removeElement(Object obj); public final void removeElementAt(int index); public final void setElementAt(Object obj, int index); public final void setSize(int newSize); public final int size(); public final String toString(); public final void trimToSize(); }
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.
protected int capacityIncrementThe amount by which the capacity of the vector is automatically incremented when its size becomes greater than its capacity. If the capacity is 0, the capacity of the vector is doubled each time it needs to grow.
protected int elementCountThe number of valid components in the vector.
protected Object elementData[]The array buffer into which the components of the vector are stored. The length of this array buffer is the (current) capacity of the vector.
public Vector()Constructs an empty vector.
public Vector(int initialCapacity)Constructs an empty vector. It initial capacity is the specified argument size.
Parameter Description initialCapacity the initial capacity of the vector
public Vector(int initialCapacity, int capacityIncrement)Constructs an empty vector with the specified capacity and the specified capacity increment.
Parameter Description initialCapacity the initial capacity of the vector capacityIncrement the amount by which the capacity is increased when the vector overflows.
public final void addElement(Object 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.
Parameter Description obj the component to be added
public final int capacity()Return Value:
Returns the current capacity of this vector.
public Object clone()Return Value:
Returns a clone of this vector.
Overrides:
clone() in class Object .
public final boolean contains(Object elem)Return Value:
Returns true if the specified object is an component in this vector; false otherwise.
Parameter Description elem an object
public final 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.
Parameter Description anArray the array into which the components get copied.
public final Object elementAt(int index)Return Value:
Returns the component at the specified index.
Parameter Description index an index into this vector Throw:
ArrayIndexOutOfBoundsException
If an invalid index was given.
public final Enumeration elements()Return Value:
Returns an enumeration of the components of this vector.
public final 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.
Parameter Description minCapacity the desired minimum capacity
public final Object firstElement()Return Value:
Returns the first component of this vector.
Throw:
If this vector has no components.
public final int indexOf(Object elem)Return Value:
Returns the index of the first occurrence of the argument in this vector; returns -1 if the object is not found.
Parameter Description elem an object
public final int indexOf(Object elem, int index)Return Value:
Returns the index of the first occurrence of the object argument in thisn vector at position index or later in the vector; returns -1 if the object is not found.
Parameter Description elem an object index the index where to start searching
public final void insertElementAt(Object obj, int index)Inserts the specified object as an 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.
Parameter Description obj the component to insert index where to insert the new component Throw:
ArrayIndexOutOfBoundsException
If the index was invalid.
public final boolean isEmpty()Return Value:
Returns true if this vector has no components; false otherwise.
public final Object lastElement()Return Value:
Returns the last component of the vector, i.e. the component at index size() - 1.
Throw:
If this vector is empty.
public final int lastIndexOf(Object elem)Return Value:
Returns the index of the last occurrence of the argument in this vector; returns -1 if the object is not found.
Parameter Description elem the desired component
public final int lastIndexOf(Object elem, int index)Searches backwards for the specified object, starting from the specified index and returns an index to it.
Return Value:
Returns the index of the last occurrence of the object argument in this vector at position less than index in the vector; returns -1 if the object is not found.
Parameter Description elem the desired component index the index where to start searching
public final void removeAllElements()Removes all components from this vector and sets its size to zero.
public final 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
Return Value:
Returns true if the argument was a component of this vector; false otherwise.
Parameter Description obj the component to be removed
public final 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.
Parameter Description index the index of the object to remove Throw:
ArrayIndexOutOfBoundsException
If the index was invalid.
public final void setElementAt(Object 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.
Parameter Description obj what the component is to be set to index the specified index Throw:
ArrayIndexOutOfBoundsException
If the index was invalid.
public final 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
Parameter Description newSize the new size of this vector
public final int size()Return Value:
Returns the number of components in this vector.
public final String toString()Creates a string representation of this vector.
Return Value:
Returns a string representation of this vector.
Overrides:
toString in class Object .
public final 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.