Packages  This Package  Prev  Next  Index  

§3.10 Class Vector

public  class  java.util.Vector
    extends  java.lang.Object  (I-§1.12)
    implements java.lang.Cloneable  (I-§1.22)
{
        // Fields
    protected int capacityIncrement;	§3.10.1
    protected int elementCount;	§3.10.2
    protected Object elementData[];	§3.10.3

        // Constructors
    public Vector();	§3.10.4
    public Vector(int  initialCapacity);	§3.10.5
    public Vector(int  initialCapacity, 	§3.10.6
                      int  capacityIncrement);

        // Methods
    public final void addElement(Object  obj);	§3.10.7
    public final int capacity();	§3.10.8
    public Object clone();	§3.10.9
    public final boolean contains(Object  elem);	§3.10.10
    public final void copyInto(Object  anArray[]);	§3.10.11
    public final Object elementAt(int  index);	§3.10.12
    public final Enumeration elements();	§3.10.13
    public final void ensureCapacity(int  minCapacity)	§3.10.14
    public final Object firstElement();	§3.10.15
    public final int indexOf(Object  elem);	§3.10.16
    public final int indexOf(Object  elem, int  index);	§3.10.17
    public final void insertElementAt(Object  obj, int  index);	§3.10.18
    public final boolean isEmpty();	§3.10.19
    public final Object lastElement();	§3.10.20
    public final int lastIndexOf(Object  elem);	§3.10.21
    public final int lastIndexOf(Object  elem, int  index);	§3.10.22
    public final void removeAllElements();	§3.10.23
    public final boolean removeElement(Object  obj);	§3.10.24
    public final void removeElementAt(int  index);	§3.10.25
    public final void setElementAt(Object  obj, int  index);	§3.10.26
    public final void setSize(int  newSize);	§3.10.27
    public final int size();	§3.10.28
    public final String toString();	§3.10.29
    public final void trimToSize();	§3.10.30
}
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.


Fields

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 is 0, the capacity of the vector is doubled each time it needs to grow.

elementCount

protected int elementCount
The number of valid components in the vector.

elementData

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.

Constructors

Vector

public Vector()
Constructs an empty vector.

Vector

public Vector(int initialCapacity)
Constructs an empty vector. It initial capacity is the specified argument size.
Parameters:
initialCapacity - the initial capacity of the vector

Vector

public Vector(int initialCapacity, int capacityIncrement)
Constructs an empty vector with the specified capacity and the specified capacity increment.
Parameters:
initialCapacity - the initial capacity of the vector
capacityIncrement - the amount by which the capacity is increased when the vector overflows.

Methods

addElement

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.
Parameters:
obj - the component to be added

capacity

public final int capacity()
Returns:
the current capacity of this vector.

clone

public Object clone()
Returns:
a clone of this vector.
Overrides:
clone() in class Object (I-§1.12.2).

contains

public final boolean contains(Object elem)
Parameters:
elem - an object
Returns:
true if the specified object is an component in this vector; false otherwise.

copyInto

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.
Parameters:
anArray - the array into which the components get copied.

elementAt

public final Object elementAt(int index)
Parameters:
index - an index into this vector
Returns:
the component at the specified index.
Throws
ArrayIndexOutOfBoundsException (I-§1.25)
If an invalid index was given.

elements

public final Enumeration elements()
Returns:
an enumeration (I-§3.11) of the components of this vector.

ensureCapacity

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.
Parameters:
minCapacity - the desired minimum capacity

firstElement

public final Object firstElement()
Returns:
the first component of this vector.
Throws
NoSuchElementException (I-§3.14)
If this vector has no components.

indexOf

public final int indexOf(Object elem)
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.

indexOf

public final int indexOf(Object elem, int index)
Parameters:
elem - an object
index - the index where to start searching
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.

insertElementAt

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 (I-§3.10.28) of the vector.

Parameters:
obj - the component to insert
index - where to insert the new component
Throws
ArrayIndexOutOfBoundsException (I-§1.25)
If the index was invalid.

isEmpty

public final boolean isEmpty()
Returns:
true if this vector has no components; false otherwise.

lastElement

public final Object lastElement()
Returns:
the last component of the vector, i.e. the component at index .
Throws
NoSuchElementException (I-§3.14)
If this vector is empty.

lastIndexOf

public final int lastIndexOf(Object elem)
Parameters:
elem - the desired component
Returns:
the index of the last occurrence of the argument in this vector; returns -1 if the object is not found.

lastIndexOf

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.
Parameters:
elem - the desired component
index - the index where to start searching
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.

removeAllElements

public final void removeAllElements()
Removes all components from this vector and sets its size to zero.

removeElement

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
Parameters:
obj - the component to be removed
Returns:
true if the argument was a component of this vector; false otherwise.

removeElementAt

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 (I-§3.10.28) of the vector.

Parameters:
index - the index of the object to remove
Throws
ArrayIndexOutOfBoundsException (I-§1.25)
If the index was invalid.

setElementAt

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 (I-§3.10.28) of the vector.

Parameters:
obj - what the component is to be set to
index - the specified index
Throws
ArrayIndexOutOfBoundsException (I-§1.25)
If the index was invalid.

setSize

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
Parameters:
newSize - the new size of this vector

size

public final int size()
Returns:
the number of components in this vector.

toString

public final String toString()
Creates a string representation of this vector.
Returns:
a string representation of this vector.
Overrides:
toString in class Object (I-§1.12.9).

trimToSize

public final void trimToSize()
Trims the capacity of this vector to be the vector's current size (I-§3.10.28). An application can use this operation to minimize the storage of a vector.


Packages  This Package  Prev  Next  Index
Java API Document (HTML generated by dkramer on April 22, 1996)
Copyright © 1996 Sun Microsystems, Inc. All rights reserved
Please send any comments or corrections to doug.kramer@sun.com