Package java.util Previous
Previous
Java API
Java API
Index
Index
Next
Next

Class Vector

Fields , Constructors , Methods

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.


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.

ParameterDescription
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.

ParameterDescription
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.

ParameterDescription
obj the component to be added


capacity

public final int capacity() 

Return Value:

Returns the current capacity of this vector.


clone

public Object clone() 

Return Value:

Returns a clone of this vector.

Overrides:

clone() in class Object .


contains

public final boolean contains(Object  elem) 

Return Value:

Returns true if the specified object is an component in this vector; false otherwise.

ParameterDescription
elem an object


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.

ParameterDescription
anArray the array into which the components get copied.


elementAt

public final Object elementAt(int  index) 

Return Value:

Returns the component at the specified index.

ParameterDescription
index an index into this vector

Throw:

ArrayIndexOutOfBoundsException

If an invalid index was given.


elements

public final Enumeration elements() 

Return Value:

Returns an enumeration 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.

ParameterDescription
minCapacity the desired minimum capacity


firstElement

public final Object firstElement() 

Return Value:

Returns the first component of this vector.

Throw:

NoSuchElementException

If this vector has no components.


indexOf

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.

ParameterDescription
elem an object


indexOf

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.

ParameterDescription
elem an object
index the index where to start searching


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 of the vector.

ParameterDescription
obj the component to insert
index where to insert the new component

Throw:

ArrayIndexOutOfBoundsException

If the index was invalid.


isEmpty

public final boolean isEmpty() 

Return Value:

Returns true if this vector has no components; false otherwise.


lastElement

public final Object lastElement() 

Return Value:

Returns the last component of the vector, i.e. the component at index size() - 1.

Throw:

NoSuchElementException

If this vector is empty.


lastIndexOf

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.

ParameterDescription
elem the desired component


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.

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.

ParameterDescription
elem the desired component
index the index where to start searching


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

Return Value:

Returns true if the argument was a component of this vector; false otherwise.

ParameterDescription
obj the component to be removed


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 of the vector.

ParameterDescription
index the index of the object to remove

Throw:

ArrayIndexOutOfBoundsException

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 of the vector.

ParameterDescription
obj what the component is to be set to
index the specified index

Throw:

ArrayIndexOutOfBoundsException

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

ParameterDescription
newSize the new size of this vector


size

public final int size() 

Return Value:

Returns the number of components in this vector.


toString

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 .


trimToSize

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.



Top© 1996 Sun Microsystems, Inc. All rights reserved.