java.util
Class Vector

java.lang.Object
  extended by java.util.Vector

public class Vector
extends Object

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.

Author:
Marcus Timmermann
Available on JControl Devices:
lib

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 this Vector object.
protected  Object[] elementData
          The array buffer into which the components of the vector are stored.
 
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 add(int index, Object element)
          Inserts the specified element at the specified position in this Vector.
 boolean add(Object o)
          Appends the specified element to the end of this Vector.
 int capacity()
          Returns the current capacity of this vector.
 void clear()
          Removes all of the elements from this Vector.
 Object clone()
          Returns a clone of this vector.
 boolean contains(Object elem)
          Tests if the specified object is a component in this vector.
 boolean equals(Object o)
          Compares the specified Object with this Vector for equality.
 Object firstElement()
          Returns the first component (the item at index 0) of this vector.
 Object get(int index)
          Returns the element at the specified position in this Vector.
 int indexOf(Object elem)
          Searches for the first occurrence 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.
 boolean isEmpty()
          Tests if this vector has no components.
 Object 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.
 Object remove(int index)
          Removes the element at the specified position in this Vector.
 boolean remove(Object o)
          Removes the first occurrence of the specified element in this Vector.
 Object set(int index, Object element)
          Replaces the element at the specified position in this Vector with the specified element.
 void setSize(int newSize)
          Sets the size of this vector.
 int size()
          Returns the number of components in this vector.
 Object[] toArray()
          Returns an array containing all of the elements in this Vector in the correct order.
 void trimToSize()
          Trims the capacity of this vector to be the vector's current size.
 
Methods inherited from class java.lang.Object
notifyAll, wait
 

Field Detail

capacityIncrement

protected int capacityIncrement
The amount by which the capacity of the vector is automatically incremented when its size becomes greater than its capacity.


elementCount

protected int elementCount
The number of valid components in this Vector object.


elementData

protected Object[] elementData
The array buffer into which the components of the vector are stored.

Constructor Detail

Vector

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


Vector

public Vector(int initialCapacity)
       throws IllegalArgumentException
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.
Throws:
IllegalArgumentException

Vector

public Vector(int initialCapacity,
              int capacityIncrement)
       throws IllegalArgumentException
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
Method Detail

add

public void add(int index,
                Object element)
Inserts the specified element at the specified position in this Vector. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Parameters:
index - index at which the specified element is to be inserted.
element - element to be inserted.

add

public boolean add(Object o)
Appends the specified element to the end of this Vector.

Parameters:
o - element to be appended to this Vector.
Returns:
true (according to the JDK1.3 specification)

capacity

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

Returns:
the current capacity (the length of its internal data arary, kept in the field elementData of this vector.

clear

public void clear()
Removes all of the elements from this Vector. The Vector will be empty after this call returns (unless it throws an exception).


clone

public Object clone()
Returns a clone of this vector. The copy will contain a reference to a clone of the internal data array, not a reference to the original internal data array of this Vector object.

Overrides:
clone in class Object
Returns:
a clone of this vector.
See Also:
Cloneable

contains

public boolean contains(Object elem)
Tests if the specified object is a component in this vector.

Parameters:
elem - an object to look for
Returns:
true, if the object was found

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 List, both Lists have the same size, and all corresponding pairs of elements in the two Lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two Lists 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

firstElement

public Object firstElement()
Returns the first component (the item at index 0) of this vector.

Returns:
the first component of this vector.

get

public Object get(int index)
Returns the element at the specified position in this Vector.

Parameters:
index - index of element to return.
Returns:
the object at the specified index

indexOf

public int indexOf(Object elem)
Searches for the first occurrence 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, that is, the smallest value k such that elem.equals(elementData[k]) is true; returns -1 if the object is not found.

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 non-negative 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, that is, the smallest value k such that elem.equals(elementData[k]) && (k >= index) is true; returns -1 if the object is not found. (Returns -1 if index >= the current size of this Vector.)

isEmpty

public boolean isEmpty()
Tests if this vector has no components.

Returns:
true if and only if this vector has no components, that is, its size is zero; false otherwise.

lastElement

public Object lastElement()
Returns the last component of the vector.

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

lastIndexOf

public int lastIndexOf(Object elem)
Returns the index of the last occurrence of the specified object in this vector.

Parameters:
elem - an object
Returns:
the index of the last occurrence of the specified object in this vector, that is, the largest value k such that elem.equals(elementData[k]) is true; returns -1 if the object is not found.

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 or equal to index in the vector, that is, the largest value k such that elem.equals(elementData[k]) && (k <= index) is true; -1 if the object is not found. (Returns -1 if index is negative.)

remove

public Object remove(int index)
Removes the element at the specified position in this Vector. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the Vector.

Parameters:
index - the index of the element to removed.
Returns:
the element that was removed.

remove

public boolean remove(Object o)
Removes the first occurrence of the specified element in this Vector. If the Vector does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists).

Parameters:
o - element to be removed from this Vector, if present.
Returns:
true if the Vector contained the specified element.

set

public Object set(int index,
                  Object element)
Replaces the element at the specified position in this Vector with the specified element.

Parameters:
index - index of element to replace.
element - element to be stored at the specified position.
Returns:
the element previously at the specified position.

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.

size

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

Returns:
the number of components in this vector.

toArray

public Object[] toArray()
Returns an array containing all of the elements in this Vector in the correct order.

Returns:
an array containing all of the elements in this Vector.

trimToSize

public void trimToSize()
Trims the capacity of this vector to be the vector's current size. If the capacity of this vector is larger than its current size, then the capacity is changed to equal the size by replacing its internal data array, kept in the field elementData, with a smaller one. An application can use this operation to minimize the storage of a vector.