Contents | Prev | Next | Index

21.11 The Class java.util.Vector

A Vector, like an array, contains items that can be accessed using an integer index. However, the size of a Vector can grow and shrink as needed to accommodate adding and removing items after the Vector has been created.

public class Vector implements Cloneable {
	protected Object[] elementData;
	protected int elementCount;
	protected int capacityIncrement;
	public Vector(int initialCapacity, int capacityIncrement);
	public Vector(int initialCapacity);
	public Vector();
	public final String toString();
	public Object clone();
	public final Object elementAt(int index)
		throws IndexOutOfBoundsException;
	public final void setElementAt(Object obj, int index)
		throws IndexOutOfBoundsException;
	public final Object firstElement()
		throws NoSuchElementException;
	public final Object lastElement()
		throws NoSuchElementException;
	public final void addElement(Object obj);
	public final void insertElementAt(Object obj, int index)
		throws IndexOutOfBoundsException;
	public final boolean removeElement(Object obj);
	public final void removeElementAt(int index)
		throws IndexOutOfBoundsException;
	public final void removeAllElements();
	public final boolean isEmpty();
	public final int size();
	public final void setSize(int newSize);
	public final int capacity();
	public final void ensureCapacity(int minCapacity);
	public final void trimToSize();
	public final void copyInto(Object anArray[])
		throws IndexOutOfBoundsException;
	public final Enumeration elements();
	public final boolean contains(Object elem);
	public final int indexOf(Object elem);
	public final int indexOf(Object elem, int index)
		throws IndexOutOfBoundsException;
	public final int lastIndexOf(Object elem);
	public final int lastIndexOf(Object elem, int index)
		throws IndexOutOfBoundsException;
}

21.11.1 protected Object[] elementData;

Internally, a Vector keeps its elements in an array that is at least large enough to contain all the elements.

21.11.2 protected int elementCount;

This field holds the number of items currently in this Vector object. Components elementData[0] through elementData[elementCount-1] are the actual items.

21.11.3 protected int capacityIncrement;

When the method ensureCapacity (§21.11.22) must increase the size of the data array in the field elementData (by creating a new array), it increases the size by at least the amount in capacityIncrement; but if capacityIncrement is zero, then it at least doubles the size of the data array.

21.11.4 public Vector(int initialCapacity, int capacityIncrement)

This constructor initializes a newly created Vector so that its internal data array has size initialCapacity and its standard capacity increment is the value of capacityIncrement. Initially, the Vector contains no items.

21.11.5 public Vector(int initialCapacity)

This constructor initializes a newly created Vector so that its internal data array has size initialCapacity and its standard capacity increment is zero. Initially, the Vector contains no items.

21.11.6 public Vector()

This constructor initializes a newly created Vector so that its internal data array has size 10 and its standard capacity increment is zero. Initially the Vector contains no items.

21.11.7 public final String toString()

This Vector is represented in string form as a list of its items, enclosed in ASCII square brackets and separated by the ASCII characters ", " (comma and space). The toString method is used to convert the items to strings; a null reference is rendered as the string "null".

The example fragment:


Vector v = new Vector();
v.addElement("Canberra");
v.addElement("Cancun");
v.addElement("Canandaigua");
System.out.println(v.toString());

produces the output:

[Canberra, Cancun, Canandaigua]
Overrides the toString method of Object (§20.1.2).

21.11.8 public Object clone()

A copy of this Vector is constructed and returned. The copy will contains a reference to a clone of the internal data array, not a reference to the original internal data array of this Vector.

Overrides the clone method of Object (§20.1.5).

21.11.9 public final Object elementAt(int index)
throws IndexOutOfBoundsException

The item of this Vector with the specified index is returned.

If the index is negative or not less than the current size of this Vector, an IndexOutOfBoundsException is thrown.

21.11.10 public final void setElementAt(Object obj, int index)
throws IndexOutOfBoundsException

The item of this Vector with the specified index is replaced with obj, so that obj is now the item at the specified index within this Vector.

If the index is negative or not less than the current size of this Vector, an IndexOutOfBoundsException is thrown.

21.11.11 public final Object firstElement()
throws NoSuchElementException

If this Vector is empty, a NoSuchElementException is thrown. Otherwise, the first item (the item at index 0) is returned.

21.11.12 public final Object lastElement()
throws NoSuchElementException

If this Vector is empty, a NoSuchElementException is thrown. Otherwise, the last item (the item at index size()-1) is returned.

21.11.13 public final void addElement(Object obj)

The size of this Vector is increased by 1 and obj becomes the new last item.

21.11.14 public final void insertElementAt(Object obj, int index)
throws IndexOutOfBoundsException

The size of this Vector is increased by 1 and obj becomes the new item at the specified index. Any item in this Vector that was previously at index k is first moved to index k+1 if and only if k is not less than index.

21.11.15 public final boolean removeElement(Object obj)

If this Vector contains an occurrence of obj, then the first (lowest-indexed) such occurrence is removed, as if by the method removeElementAt (§21.11.16), and true is returned. If this Vector contains no occurrence of obj, this Vector is not modified and false is returned.

21.11.16 public final void removeElementAt(int index)
throws IndexOutOfBoundsException

The size of this Vector is decreased by 1 and the item at the specified index is removed from this Vector. Any item in this Vector that was previously at index k is first moved to index k-1 if and only if k is greater than index.

21.11.17 public final void removeAllElements()

All elements are removed from this Vector, making it empty.

21.11.18 public final boolean isEmpty()

The result is true if and only if this Vector is empty, that is, its size is zero.

21.11.19 public final int size()

The size of this Vector (the number of items it currently contains) is returned.

21.11.20 public final void setSize(int newSize)

The size of this Vector is changed to newSize. If the new size is smaller than the old size, then items are removed from the end and discarded. If the new size is larger than the old size, then the new items are set to null.

21.11.21 public final int capacity()

The current capacity of this Vector (the length of its internal data array, kept in the field elementData) is returned.

21.11.22 public final void ensureCapacity(int minCapacity)

If the current capacity of this Vector is less than minCapacity, then its capacity is increased by replacing its internal data array, kept in the field elementData (§21.11.1), with a larger one. The size of the new data array will be the old size plus capacityIncrement (§21.11.3), unless the value of capacityIncrement is nonpositive, in which case the new capacity will be twice the old capacity; but if this new size is still smaller than minCapacity, then the new capacity will be minCapacity.

21.11.23 public final void trimToSize()

If the capacity of this Vector is larger than its current size (§21.11.19), then the capacity is changed to equal the size by replacing its internal data array, kept in the field elementData, with a smaller one.

21.11.24 public final void copyInto(Object anArray[])
throws IndexOutOfBoundsException

All the items in this Vector are copied into the array anArray. The item at index k in this Vector is copied into component k of anArray. If the length of anArray is smaller than the size of this Vector, an IndexOutOfBoundsException is thrown.

21.11.25 public final Enumeration elements()

An Enumeration (§21.1) is returned that will generate all items in this Vector. The first item generated is the item at index 0, then the item at index 1, and so on.

21.11.26 public final boolean contains(Object elem)

The result is true if and only if some item in this Vector is the same as elem, as determined by the equals method (§20.1.3).

21.11.27 public final int indexOf(Object elem)

If an item equal to elem is in this Vector, then the index of the first such occurrence is returned, that is, the smallest value k such that:

elem.equals(elementData[k])
is true. If no such item occurs in this Vector, then -1 is returned.

21.11.28 public final int indexOf(Object elem, int index)
throws IndexOutOfBoundsException

If an item equal to elem is in this Vector at position k or higher, then the index of the first such occurrence is returned, that is, the smallest value k such that:

elem.equals(elementData[k]) && (k >= index)
is true. If no such item occurs in this Vector, then -1 is returned.

21.11.29 public final int lastIndexOf(Object elem)

If an item equal to elem is in this Vector, then the index of the last such occurrence is returned, that is, the largest value k such that:

elem.equals(elementData[k])
is true. If no such item occurs in this Vector, then -1 is returned.

21.11.30 public final int lastIndexOf(Object elem, int index)
throws IndexOutOfBoundsException

If an item equal to elem is in this Vector at position k or lower, then the index of the last such occurrence is returned, that is, the largest value k such that:

elem.equals(elementData[k]) && (k <= index)
is true. If no such item occurs in this Vector, then -1 is returned.


Contents | Prev | Next | Index

Java Language Specification (HTML generated by Suzette Pelouch on February 24, 1998)
Copyright © 1996 Sun Microsystems, Inc. All rights reserved
Please send any comments or corrections to doug.kramer@sun.com