java.util
Class Vector<E>

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.util.AbstractCollection sample code for java.util.AbstractCollection definition code for java.util.AbstractCollection <E>
      extended by java.util.AbstractList sample code for java.util.AbstractList definition code for java.util.AbstractList <E>
          extended by java.util.Vector<E>
All Implemented Interfaces:
Serializable sample code for java.io.Serializable definition code for java.io.Serializable , Cloneable sample code for java.lang.Cloneable definition code for java.lang.Cloneable , Iterable sample code for java.lang.Iterable definition code for java.lang.Iterable <E>, Collection sample code for java.util.Collection definition code for java.util.Collection <E>, List sample code for java.util.List definition code for java.util.List <E>, RandomAccess sample code for java.util.RandomAccess definition code for java.util.RandomAccess
Direct Known Subclasses:
Stack sample code for java.util.Stack definition code for java.util.Stack

public class Vector<E>
extends AbstractList sample code for java.util.AbstractList definition code for java.util.AbstractList <E>
implements List sample code for java.util.List definition code for java.util.List <E>, RandomAccess sample code for java.util.RandomAccess definition code for java.util.RandomAccess , Cloneable sample code for java.lang.Cloneable definition code for java.lang.Cloneable , Serializable sample code for java.io.Serializable definition code for java.io.Serializable

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.

As of the Java 2 platform v1.2, this class has been retrofitted to implement List, so that it becomes a part of Java's collection framework. Unlike the new collection implementations, Vector is synchronized.

The Iterators returned by Vector's iterator and listIterator methods are fail-fast: if the Vector is structurally modified at any time after the Iterator is created, in any way except through the Iterator's own remove or add methods, the Iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the Iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future. The Enumerations returned by Vector's elements method are not fail-fast.

Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.

This class is a member of the Java Collections Framework.

Since:
JDK1.0
See Also:
Collection sample code for java.util.Collection definition code for java.util.Collection , List sample code for java.util.List definition code for java.util.List , ArrayList sample code for java.util.ArrayList definition code for java.util.ArrayList , LinkedList sample code for java.util.LinkedList definition code for java.util.LinkedList , Serialized Form

Field Summary
protected  int capacityIncrement sample code for java.util.Vector.capacityIncrement definition code for java.util.Vector.capacityIncrement
          The amount by which the capacity of the vector is automatically incremented when its size becomes greater than its capacity.
protected  int elementCount sample code for java.util.Vector.elementCount definition code for java.util.Vector.elementCount
          The number of valid components in this Vector object.
protected  Object sample code for java.lang.Object definition code for java.lang.Object [] elementData sample code for java.util.Vector.elementData definition code for java.util.Vector.elementData
          The array buffer into which the components of the vector are stored.
 
Fields inherited from class java.util.AbstractList sample code for java.util.AbstractList definition code for java.util.AbstractList
modCount sample code for java.util.AbstractList.modCount definition code for java.util.AbstractList.modCount
 
Constructor Summary
Vector sample code for java.util.Vector.Vector() definition code for java.util.Vector.Vector() ()
          Constructs an empty vector so that its internal data array has size 10 and its standard capacity increment is zero.
Vector sample code for java.util.Vector.Vector(java.util.Collection) definition code for java.util.Vector.Vector(java.util.Collection) (Collection sample code for java.util.Collection definition code for java.util.Collection <? extends E> c)
          Constructs a vector containing the elements of the specified collection, in the order they are returned by the collection's iterator.
Vector sample code for java.util.Vector.Vector(int) definition code for java.util.Vector.Vector(int) (int initialCapacity)
          Constructs an empty vector with the specified initial capacity and with its capacity increment equal to zero.
Vector sample code for java.util.Vector.Vector(int, int) definition code for java.util.Vector.Vector(int, int) (int initialCapacity, int capacityIncrement)
          Constructs an empty vector with the specified initial capacity and capacity increment.
 
Method Summary
 boolean add sample code for java.util.Vector.add(E) definition code for java.util.Vector.add(E) (E o)
          Appends the specified element to the end of this Vector.
 void add sample code for java.util.Vector.add(int, E) definition code for java.util.Vector.add(int, E) (int index, E element)
          Inserts the specified element at the specified position in this Vector.
 boolean addAll sample code for java.util.Vector.addAll(java.util.Collection) definition code for java.util.Vector.addAll(java.util.Collection) (Collection sample code for java.util.Collection definition code for java.util.Collection <? extends E> c)
          Appends all of the elements in the specified Collection to the end of this Vector, in the order that they are returned by the specified Collection's Iterator.
 boolean addAll sample code for java.util.Vector.addAll(int, java.util.Collection) definition code for java.util.Vector.addAll(int, java.util.Collection) (int index, Collection sample code for java.util.Collection definition code for java.util.Collection <? extends E> c)
          Inserts all of the elements in the specified Collection into this Vector at the specified position.
 void addElement sample code for java.util.Vector.addElement(E) definition code for java.util.Vector.addElement(E) (E obj)
          Adds the specified component to the end of this vector, increasing its size by one.
 int capacity sample code for java.util.Vector.capacity() definition code for java.util.Vector.capacity() ()
          Returns the current capacity of this vector.
 void clear sample code for java.util.Vector.clear() definition code for java.util.Vector.clear() ()
          Removes all of the elements from this Vector.
 Object sample code for java.lang.Object definition code for java.lang.Object clone sample code for java.util.Vector.clone() definition code for java.util.Vector.clone() ()
          Returns a clone of this vector.
 boolean contains sample code for java.util.Vector.contains(java.lang.Object) definition code for java.util.Vector.contains(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  elem)
          Tests if the specified object is a component in this vector.
 boolean containsAll sample code for java.util.Vector.containsAll(java.util.Collection) definition code for java.util.Vector.containsAll(java.util.Collection) (Collection sample code for java.util.Collection definition code for java.util.Collection <?> c)
          Returns true if this Vector contains all of the elements in the specified Collection.
 void copyInto sample code for java.util.Vector.copyInto(java.lang.Object[]) definition code for java.util.Vector.copyInto(java.lang.Object[]) (Object sample code for java.lang.Object definition code for java.lang.Object [] anArray)
          Copies the components of this vector into the specified array.
 E elementAt sample code for java.util.Vector.elementAt(int) definition code for java.util.Vector.elementAt(int) (int index)
          Returns the component at the specified index.
 Enumeration sample code for java.util.Enumeration definition code for java.util.Enumeration <E> elements sample code for java.util.Vector.elements() definition code for java.util.Vector.elements() ()
          Returns an enumeration of the components of this vector.
 void ensureCapacity sample code for java.util.Vector.ensureCapacity(int) definition code for java.util.Vector.ensureCapacity(int) (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.
 boolean equals sample code for java.util.Vector.equals(java.lang.Object) definition code for java.util.Vector.equals(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  o)
          Compares the specified Object with this Vector for equality.
 E firstElement sample code for java.util.Vector.firstElement() definition code for java.util.Vector.firstElement() ()
          Returns the first component (the item at index 0) of this vector.
 E get sample code for java.util.Vector.get(int) definition code for java.util.Vector.get(int) (int index)
          Returns the element at the specified position in this Vector.
 int hashCode sample code for java.util.Vector.hashCode() definition code for java.util.Vector.hashCode() ()
          Returns the hash code value for this Vector.
 int indexOf sample code for java.util.Vector.indexOf(java.lang.Object) definition code for java.util.Vector.indexOf(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  elem)
          Searches for the first occurence of the given argument, testing for equality using the equals method.
 int indexOf sample code for java.util.Vector.indexOf(java.lang.Object, int) definition code for java.util.Vector.indexOf(java.lang.Object, int) (Object sample code for java.lang.Object definition code for java.lang.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.
 void insertElementAt sample code for java.util.Vector.insertElementAt(E, int) definition code for java.util.Vector.insertElementAt(E, int) (E obj, int index)
          Inserts the specified object as a component in this vector at the specified index.
 boolean isEmpty sample code for java.util.Vector.isEmpty() definition code for java.util.Vector.isEmpty() ()
          Tests if this vector has no components.
 E lastElement sample code for java.util.Vector.lastElement() definition code for java.util.Vector.lastElement() ()
          Returns the last component of the vector.
 int lastIndexOf sample code for java.util.Vector.lastIndexOf(java.lang.Object) definition code for java.util.Vector.lastIndexOf(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  elem)
          Returns the index of the last occurrence of the specified object in this vector.
 int lastIndexOf sample code for java.util.Vector.lastIndexOf(java.lang.Object, int) definition code for java.util.Vector.lastIndexOf(java.lang.Object, int) (Object sample code for java.lang.Object definition code for java.lang.Object  elem, int index)
          Searches backwards for the specified object, starting from the specified index, and returns an index to it.
 E remove sample code for java.util.Vector.remove(int) definition code for java.util.Vector.remove(int) (int index)
          Removes the element at the specified position in this Vector.
 boolean remove sample code for java.util.Vector.remove(java.lang.Object) definition code for java.util.Vector.remove(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  o)
          Removes the first occurrence of the specified element in this Vector If the Vector does not contain the element, it is unchanged.
 boolean removeAll sample code for java.util.Vector.removeAll(java.util.Collection) definition code for java.util.Vector.removeAll(java.util.Collection) (Collection sample code for java.util.Collection definition code for java.util.Collection <?> c)
          Removes from this Vector all of its elements that are contained in the specified Collection.
 void removeAllElements sample code for java.util.Vector.removeAllElements() definition code for java.util.Vector.removeAllElements() ()
          Removes all components from this vector and sets its size to zero.
 boolean removeElement sample code for java.util.Vector.removeElement(java.lang.Object) definition code for java.util.Vector.removeElement(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  obj)
          Removes the first (lowest-indexed) occurrence of the argument from this vector.
 void removeElementAt sample code for java.util.Vector.removeElementAt(int) definition code for java.util.Vector.removeElementAt(int) (int index)
          Deletes the component at the specified index.
protected  void removeRange sample code for java.util.Vector.removeRange(int, int) definition code for java.util.Vector.removeRange(int, int) (int fromIndex, int toIndex)
          Removes from this List all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive.
 boolean retainAll sample code for java.util.Vector.retainAll(java.util.Collection) definition code for java.util.Vector.retainAll(java.util.Collection) (Collection sample code for java.util.Collection definition code for java.util.Collection <?> c)
          Retains only the elements in this Vector that are contained in the specified Collection.
 E set sample code for java.util.Vector.set(int, E) definition code for java.util.Vector.set(int, E) (int index, E element)
          Replaces the element at the specified position in this Vector with the specified element.
 void setElementAt sample code for java.util.Vector.setElementAt(E, int) definition code for java.util.Vector.setElementAt(E, int) (E obj, int index)
          Sets the component at the specified index of this vector to be the specified object.
 void setSize sample code for java.util.Vector.setSize(int) definition code for java.util.Vector.setSize(int) (int newSize)
          Sets the size of this vector.
 int size sample code for java.util.Vector.size() definition code for java.util.Vector.size() ()
          Returns the number of components in this vector.
 List sample code for java.util.List definition code for java.util.List <E> subList sample code for java.util.Vector.subList(int, int) definition code for java.util.Vector.subList(int, int) (int fromIndex, int toIndex)
          Returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive.
 Object sample code for java.lang.Object definition code for java.lang.Object [] toArray sample code for java.util.Vector.toArray() definition code for java.util.Vector.toArray() ()
          Returns an array containing all of the elements in this Vector in the correct order.
<T> T[]
toArray sample code for java.util.Vector.toArray(T[]) definition code for java.util.Vector.toArray(T[]) (T[] a)
          Returns an array containing all of the elements in this Vector in the correct order; the runtime type of the returned array is that of the specified array.
 String sample code for java.lang.String definition code for java.lang.String toString sample code for java.util.Vector.toString() definition code for java.util.Vector.toString() ()
          Returns a string representation of this Vector, containing the String representation of each element.
 void trimToSize sample code for java.util.Vector.trimToSize() definition code for java.util.Vector.trimToSize() ()
          Trims the capacity of this vector to be the vector's current size.
 
Methods inherited from class java.util.AbstractList sample code for java.util.AbstractList definition code for java.util.AbstractList
iterator sample code for java.util.AbstractList.iterator() definition code for java.util.AbstractList.iterator() , listIterator sample code for java.util.AbstractList.listIterator() definition code for java.util.AbstractList.listIterator() , listIterator sample code for java.util.AbstractList.listIterator(int) definition code for java.util.AbstractList.listIterator(int)
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 
Methods inherited from interface java.util.List sample code for java.util.List definition code for java.util.List
iterator sample code for java.util.List.iterator() definition code for java.util.List.iterator() , listIterator sample code for java.util.List.listIterator() definition code for java.util.List.listIterator() , listIterator sample code for java.util.List.listIterator(int) definition code for java.util.List.listIterator(int)
 

Field Detail

elementData sample code for java.util.Vector.elementData

protected Object sample code for java.lang.Object definition code for java.lang.Object [] elementData
The array buffer into which the components of the vector are stored. The capacity of the vector is the length of this array buffer, and is at least large enough to contain all the vector's elements.

Any array elements following the last element in the Vector are null.


elementCount sample code for java.util.Vector.elementCount

protected int elementCount
The number of valid components in this Vector object. Components elementData[0] through elementData[elementCount-1] are the actual items.


capacityIncrement sample code for java.util.Vector.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 increment is less than or equal to zero, the capacity of the vector is doubled each time it needs to grow.

Constructor Detail

Vector sample code for java.util.Vector(int, int) definition code for java.util.Vector(int, int)

public Vector(int initialCapacity,
              int capacityIncrement)
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 sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if the specified initial capacity is negative

Vector sample code for java.util.Vector(int) definition code for java.util.Vector(int)

public Vector(int initialCapacity)
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 sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if the specified initial capacity is negative

Vector sample code for java.util.Vector() definition code for java.util.Vector()

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


Vector sample code for java.util.Vector(java.util.Collection<? extends E>) definition code for java.util.Vector(java.util.Collection<? extends E>)

public Vector(Collection sample code for java.util.Collection definition code for java.util.Collection <? extends E> c)
Constructs a vector containing the elements of the specified collection, in the order they are returned by the collection's iterator.

Parameters:
c - the collection whose elements are to be placed into this vector.
Throws:
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if the specified collection is null.
Since:
1.2
Method Detail

copyInto sample code for java.util.Vector.copyInto(java.lang.Object[]) definition code for java.util.Vector.copyInto(java.lang.Object[])

public void copyInto(Object sample code for java.lang.Object definition code for java.lang.Object [] anArray)
Copies the components of this vector into the specified array. The item at index k in this vector is copied into component k of anArray. The array must be big enough to hold all the objects in this vector, else an IndexOutOfBoundsException is thrown.

Parameters:
anArray - the array into which the components get copied.
Throws:
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if the given array is null.

trimToSize sample code for java.util.Vector.trimToSize() definition code for java.util.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.


ensureCapacity sample code for java.util.Vector.ensureCapacity(int) definition code for java.util.Vector.ensureCapacity(int)

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

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, with a larger one. The size of the new data array will be the old size plus capacityIncrement, unless the value of capacityIncrement is less than or equal to zero, 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.

Parameters:
minCapacity - the desired minimum capacity.

setSize sample code for java.util.Vector.setSize(int) definition code for java.util.Vector.setSize(int)

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.
Throws:
ArrayIndexOutOfBoundsException sample code for java.lang.ArrayIndexOutOfBoundsException definition code for java.lang.ArrayIndexOutOfBoundsException - if new size is negative.

capacity sample code for java.util.Vector.capacity() definition code for java.util.Vector.capacity()

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

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

size sample code for java.util.Vector.size() definition code for java.util.Vector.size()

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

Specified by:
size sample code for java.util.Collection.size() definition code for java.util.Collection.size() in interface Collection sample code for java.util.Collection definition code for java.util.Collection <E>
Specified by:
size sample code for java.util.List.size() definition code for java.util.List.size() in interface List sample code for java.util.List definition code for java.util.List <E>
Specified by:
size sample code for java.util.AbstractCollection.size() definition code for java.util.AbstractCollection.size() in class AbstractCollection sample code for java.util.AbstractCollection definition code for java.util.AbstractCollection <E>
Returns:
the number of components in this vector.

isEmpty sample code for java.util.Vector.isEmpty() definition code for java.util.Vector.isEmpty()

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

Specified by:
isEmpty sample code for java.util.Collection.isEmpty() definition code for java.util.Collection.isEmpty() in interface Collection sample code for java.util.Collection definition code for java.util.Collection <E>
Specified by:
isEmpty sample code for java.util.List.isEmpty() definition code for java.util.List.isEmpty() in interface List sample code for java.util.List definition code for java.util.List <E>
Overrides:
isEmpty sample code for java.util.AbstractCollection.isEmpty() definition code for java.util.AbstractCollection.isEmpty() in class AbstractCollection sample code for java.util.AbstractCollection definition code for java.util.AbstractCollection <E>
Returns:
true if and only if this vector has no components, that is, its size is zero; false otherwise.

elements sample code for java.util.Vector.elements() definition code for java.util.Vector.elements()

public Enumeration sample code for java.util.Enumeration definition code for java.util.Enumeration <E> elements()
Returns an enumeration of the components of this vector. The returned Enumeration object 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.

Returns:
an enumeration of the components of this vector.
See Also:
Enumeration sample code for java.util.Enumeration definition code for java.util.Enumeration , Iterator sample code for java.util.Iterator definition code for java.util.Iterator

contains sample code for java.util.Vector.contains(java.lang.Object) definition code for java.util.Vector.contains(java.lang.Object)

public boolean contains(Object sample code for java.lang.Object definition code for java.lang.Object  elem)
Tests if the specified object is a component in this vector.

Specified by:
contains sample code for java.util.Collection.contains(java.lang.Object) definition code for java.util.Collection.contains(java.lang.Object) in interface Collection sample code for java.util.Collection definition code for java.util.Collection <E>
Specified by:
contains sample code for java.util.List.contains(java.lang.Object) definition code for java.util.List.contains(java.lang.Object) in interface List sample code for java.util.List definition code for java.util.List <E>
Overrides:
contains sample code for java.util.AbstractCollection.contains(java.lang.Object) definition code for java.util.AbstractCollection.contains(java.lang.Object) in class AbstractCollection sample code for java.util.AbstractCollection definition code for java.util.AbstractCollection <E>
Parameters:
elem - an object.
Returns:
true if and only if the specified object is the same as a component in this vector, as determined by the equals method; false otherwise.

indexOf sample code for java.util.Vector.indexOf(java.lang.Object) definition code for java.util.Vector.indexOf(java.lang.Object)

public int indexOf(Object sample code for java.lang.Object definition code for java.lang.Object  elem)
Searches for the first occurence of the given argument, testing for equality using the equals method.

Specified by:
indexOf sample code for java.util.List.indexOf(java.lang.Object) definition code for java.util.List.indexOf(java.lang.Object) in interface List sample code for java.util.List definition code for java.util.List <E>
Overrides:
indexOf sample code for java.util.AbstractList.indexOf(java.lang.Object) definition code for java.util.AbstractList.indexOf(java.lang.Object) in class AbstractList sample code for java.util.AbstractList definition code for java.util.AbstractList <E>
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.
See Also:
Object.equals(Object) sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object)