public class Vector<T> extends AbstractList<T> implements List<T>, RandomAccess, Cloneable
Vector classes implements growable arrays of Objects.
You can access elements in a Vector with an index, just as you
can in a built in array, but Vectors can grow and shrink to accommodate
more or fewer objects.
Vectors try to mantain efficiency in growing by having a
capacityIncrement that can be specified at instantiation.
When a Vector can no longer hold a new Object, it grows by the amount
in capacityIncrement. If this value is 0, the vector doubles in
size.
Vector implements the JDK 1.2 List interface, and is therefore a fully
compliant Collection object. The iterators are fail-fast - if external
code structurally modifies the vector, any operation on the iterator will
then throw a ConcurrentModificationException. The Vector class is
fully synchronized, but the iterators are not. So, when iterating over a
vector, be sure to synchronize on the vector itself. If you don't want the
expense of synchronization, use ArrayList instead. On the other hand, the
Enumeration of elements() is not thread-safe, nor is it fail-fast; so it
can lead to undefined behavior even in a single thread if you modify the
vector during iteration.
Note: Some methods, especially those specified by List, specify throwing
IndexOutOfBoundsException, but it is easier to implement by
throwing the subclass ArrayIndexOutOfBoundsException. Others
directly specify this subclass.
Collection,
List,
ArrayList,
LinkedList| Constructor and Description |
|---|
Vector()
Constructs an empty vector with an initial size of 5, and
a capacity increment of 0
|
Vector(Collection<? extends T> c)
Constructs a vector containing the contents of Collection, in the
order given by the collection.
|
Vector(int initialCapacity)
Constructs a Vector with the initial capacity specified, and a capacity
increment of 0 (double in size).
|
Vector(int initialCapacity,
int capacityIncrement)
Constructs a Vector with the initial capacity and capacity
increment specified.
|
| Modifier and Type | Method and Description |
|---|---|
void |
add(int index,
T element)
Adds an object at the specified index.
|
boolean |
add(T o)
Adds an object to the Vector.
|
boolean |
addAll(Collection<? extends T> c)
Appends all elements of the given collection to the end of this Vector.
|
boolean |
addAll(int index,
Collection<? extends T> c)
Inserts all elements of the given collection at the given index of
this Vector.
|
void |
addElement(T obj)
Adds an element to the Vector at the end of the Vector.
|
int |
capacity()
Returns the size of the internal data array (not the amount of elements
contained in the Vector).
|
void |
clear()
Clears all elements in the Vector and sets its size to 0.
|
Object |
clone()
Creates a new Vector with the same contents as this one.
|
boolean |
contains(Object elem)
Returns true when
elem is contained in this Vector. |
boolean |
containsAll(Collection<?> c)
Returns true if this Vector contains all the elements in c.
|
void |
copyInto(Object[] a)
Copies the contents of the Vector into the provided array.
|
T |
elementAt(int index)
Returns the Object stored at
index. |
Enumeration<T> |
elements()
Returns an Enumeration of the elements of this Vector.
|
void |
ensureCapacity(int minCapacity)
Ensures that
minCapacity elements can fit within this Vector. |
boolean |
equals(Object o)
Compares this to the given object.
|
T |
firstElement()
Returns the first element (index 0) in the Vector.
|
T |
get(int index)
Returns the element at position
index. |
int |
hashCode()
Computes the hashcode of this object.
|
int |
indexOf(Object elem)
Returns the first occurrence of
elem in the Vector, or -1 if
elem is not found. |
int |
indexOf(Object e,
int index)
Searches the vector starting at
index for object
elem and returns the index of the first occurrence of this
Object. |
void |
insertElementAt(T obj,
int index)
Inserts a new element into the Vector at
index. |
boolean |
isEmpty()
Returns true if this Vector is empty, false otherwise
|
T |
lastElement()
Returns the last element in the Vector.
|
int |
lastIndexOf(Object elem)
Returns the last index of
elem within this Vector, or -1
if the object is not within the Vector. |
int |
lastIndexOf(Object e,
int index)
Returns the index of the first occurrence of
elem, when
searching backwards from index. |
T |
remove(int index)
Removes the element at the specified index, and returns it.
|
boolean |
remove(Object o)
Removes the given Object from the Vector.
|
boolean |
removeAll(Collection<?> c)
Remove from this vector all elements contained in the given collection.
|
void |
removeAllElements()
Removes all elements from the Vector.
|
boolean |
removeElement(Object obj)
Removes the first (the lowest index) occurrence of the given object from
the Vector.
|
void |
removeElementAt(int index)
Removes the element at
index, and shifts all elements at
positions greater than index to their index - 1. |
boolean |
retainAll(Collection<?> c)
Retain in this vector only the elements contained in the given collection.
|
T |
set(int index,
T element)
Puts
element into the Vector at position index
and returns the Object that previously occupied that position. |
void |
setElementAt(T obj,
int index)
Changes the element at
index to be obj |
void |
setSize(int newSize)
Explicitly sets the size of the vector (but not necessarily the size of
the internal data array).
|
int |
size()
Returns the number of elements stored in this Vector.
|
List<T> |
subList(int fromIndex,
int toIndex)
Obtain a List view of a subsection of this list, from fromIndex
(inclusive) to toIndex (exclusive).
|
Object[] |
toArray()
Returns an Object array with the contents of this Vector, in the order
they are stored within this Vector.
|
<S> S[] |
toArray(S[] a)
Returns an array containing the contents of this Vector.
|
String |
toString()
Returns a string representation of this Vector in the form
"[element0, element1, ...
|
void |
trimToSize()
Trims the Vector down to size.
|
iterator, listIterator, listIteratoriterator, listIterator, listIteratorpublic Vector()
public Vector(Collection<? extends T> c)
c - collection of elements to add to the new vectorNullPointerException - if c is nullpublic Vector(int initialCapacity)
initialCapacity - the initial size of the Vector's internal arrayIllegalArgumentException - if initialCapacity < 0public Vector(int initialCapacity,
int capacityIncrement)
initialCapacity - the initial size of the Vector's internal arraycapacityIncrement - the amount the internal array should be
increased by when necessary, 0 to double the sizeIllegalArgumentException - if initialCapacity < 0public void add(int index,
T element)
add in interface List<T>add in class AbstractList<T>index - the index at which to add the elementelement - the element to add to the VectorArrayIndexOutOfBoundsException - index < 0 || index > size()AbstractList.modCountpublic boolean add(T o)
add in interface Collection<T>add in interface List<T>add in class AbstractList<T>o - the element to add to the VectorAbstractList.add(int, Object)public boolean addAll(Collection<? extends T> c)
addAll in interface Collection<T>addAll in interface List<T>addAll in class AbstractCollection<T>c - the collection to appendNullPointerException - if c is nullAbstractCollection.add(Object)public boolean addAll(int index,
Collection<? extends T> c)
addAll in interface List<T>addAll in class AbstractList<T>c - the collection to appendindex - the location to insert the collectionNullPointerException - if c is nullArrayIndexOutOfBoundsException - index < 0 || index > size()AbstractList.add(int, Object)public void addElement(T obj)
obj - the object to add to the Vectorpublic int capacity()
public void clear()
clear in interface Collection<T>clear in interface List<T>clear in class AbstractList<T>AbstractList.remove(int),
AbstractList.removeRange(int, int)public Object clone()
public boolean contains(Object elem)
elem is contained in this Vector.contains in interface Collection<T>contains in interface List<T>contains in class AbstractCollection<T>elem - the element to checkpublic boolean containsAll(Collection<?> c)
containsAll in interface Collection<T>containsAll in interface List<T>containsAll in class AbstractCollection<T>c - the collection to compare toNullPointerException - if c is nullAbstractCollection.contains(Object)public void copyInto(Object[] a)
IndexOutOfBoundsException is thrown without modifying the array.
Old elements in the array are overwritten by the new elements.a - target array for the copyIndexOutOfBoundsException - the array is not large enoughNullPointerException - the array is nulltoArray(Object[])public T elementAt(int index)
index.index - the index of the Object to retrieveindexArrayIndexOutOfBoundsException - index < 0 || index >= size()get(int)public Enumeration<T> elements()
AbstractList.iterator()public void ensureCapacity(int minCapacity)
minCapacity elements can fit within this Vector.
If elementData is too small, it is expanded as follows:
If the elementCount + capacityIncrement is adequate, that
is the new size. If capacityIncrement is non-zero, the
candidate size is double the current. If that is not enough, the new
size is minCapacity.minCapacity - the desired minimum capacity, negative values ignoredpublic boolean equals(Object o)
equals in interface Collection<T>equals in interface List<T>equals in class AbstractList<T>o - the object to compare toObject.equals(Object),
AbstractList.hashCode()public T firstElement()
NoSuchElementException - the Vector is emptypublic T get(int index)
index.get in interface List<T>get in class AbstractList<T>index - the position from which an element will be retrievedArrayIndexOutOfBoundsException - index < 0 || index >= size()public int hashCode()
hashCode in interface Collection<T>hashCode in interface List<T>hashCode in class AbstractList<T>Object.hashCode(),
AbstractList.equals(Object)public int indexOf(Object elem)
elem in the Vector, or -1 if
elem is not found.public int indexOf(Object e, int index)
index for object
elem and returns the index of the first occurrence of this
Object. If the object is not found, or index is larger than the size
of the vector, -1 is returned.e - the Object to search forindex - start searching at this indexIndexOutOfBoundsException - if index < 0public void insertElementAt(T obj, int index)
index. Any elements
at or greater than index are shifted up one position.obj - the object to insertindex - the index at which the object is insertedArrayIndexOutOfBoundsException - index < 0 || index > size()add(int, Object)public boolean isEmpty()
isEmpty in interface Collection<T>isEmpty in interface List<T>isEmpty in class AbstractCollection<T>AbstractCollection.size()public T lastElement()
NoSuchElementException - the Vector is emptypublic int lastIndexOf(Object elem)
elem within this Vector, or -1
if the object is not within the Vector.lastIndexOf in interface List<T>lastIndexOf in class AbstractList<T>elem - the object to search forpublic int lastIndexOf(Object e, int index)
elem, when
searching backwards from index. If the object does not
occur in this Vector, or index is less than 0, -1 is returned.e - the object to search forindex - the index to start searching in reverse fromIndexOutOfBoundsException - if index >= size()public T remove(int index)
remove in interface List<T>remove in class AbstractList<T>index - the position from which to remove the elementArrayIndexOutOfBoundsException - index < 0 || index >= size()AbstractList.modCountpublic boolean remove(Object o)
remove in interface Collection<T>remove in interface List<T>remove in class AbstractCollection<T>o - the object to remove from the VectorIterator.remove()public boolean removeAll(Collection<?> c)
removeAll in interface Collection<T>removeAll in interface List<T>removeAll in class AbstractCollection<T>c - the collection to filter outNullPointerException - if c is nullIterator.remove()public void removeAllElements()
clear()public boolean removeElement(Object obj)
obj - the object to remove from the Vectorremove(Object)public void removeElementAt(int index)
index, and shifts all elements at
positions greater than index to their index - 1.index - the index of the element to removeArrayIndexOutOfBoundsException - index < 0 || index >= size();remove(int)public boolean retainAll(Collection<?> c)
retainAll in interface Collection<T>retainAll in interface List<T>retainAll in class AbstractCollection<T>c - the collection to filter byNullPointerException - if c is nullIterator.remove()public T set(int index, T element)
element into the Vector at position index
and returns the Object that previously occupied that position.set in interface List<T>set in class AbstractList<T>index - the index within the Vector to place the Objectelement - the Object to store in the VectorArrayIndexOutOfBoundsException - index < 0 || index >= size()public void setElementAt(T obj, int index)
index to be objobj - the object to storeindex - the position in the Vector to store the objectArrayIndexOutOfBoundsException - the index is out of rangeset(int, Object)public void setSize(int newSize)
newSize - The new size of the internal arrayArrayIndexOutOfBoundsException - if the new size is negativepublic int size()
size in interface Collection<T>size in interface List<T>size in class AbstractCollection<T>public List<T> subList(int fromIndex, int toIndex)
subList in interface List<T>subList in class AbstractList<T>fromIndex - the index that the returned list should start from
(inclusive)toIndex - the index that the returned list should go to (exclusive)IndexOutOfBoundsException - if fromIndex < 0
|| toIndex > size()IllegalArgumentException - if fromIndex > toIndexConcurrentModificationExceptionpublic Object[] toArray()
toArray in interface Collection<T>toArray in interface List<T>toArray in class AbstractCollection<T>public <S> S[] toArray(S[] a)
toArray in interface Collection<T>toArray in interface List<T>toArray in class AbstractCollection<T>a - an array to copy the Vector into if large enoughArrayStoreException - the runtime type of the provided array
cannot hold the elements of the VectorNullPointerException - if a is nullpublic String toString()
toString in class AbstractCollection<T>public void trimToSize()