public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess
Objects may be inserted at any position up to the size of the vector, thus increasing the size of the vector. Objects at any position in the vector may be removed, thus shrinking the size of the Vector. Objects at any position in the Vector may be replaced, which does not affect the vector's size.
The capacity of a vector may be specified when the vector is created. If the capacity of the vector is exceeded, the capacity is increased (doubled by default).
StringBuffer
Modifier and Type | Field and Description |
---|---|
protected int |
capacityIncrement
How many elements should be added to the vector when it is detected that
it needs to grow to accommodate extra entries.
|
protected int |
elementCount
The number of elements or the size of the vector.
|
protected Object[] |
elementData
The elements of the vector.
|
modCount
Constructor and Description |
---|
Vector()
Constructs a new vector using the default capacity.
|
Vector(Collection<? extends E> collection)
Constructs a new instance of
Vector containing the elements in
collection . |
Vector(int capacity)
Constructs a new vector using the specified capacity.
|
Vector(int capacity,
int capacityIncrement)
Constructs a new vector using the specified capacity and capacity
increment.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E object)
Adds the specified object at the end of this vector.
|
void |
add(int location,
E object)
Adds the specified object into this vector at the specified location.
|
boolean |
addAll(Collection<? extends E> collection)
Adds the objects in the specified collection to the end of this vector.
|
boolean |
addAll(int location,
Collection<? extends E> collection)
Inserts the objects in the specified collection at the specified location
in this vector.
|
void |
addElement(E object)
Adds the specified object at the end of this vector.
|
int |
capacity()
Returns the number of elements this vector can hold without growing.
|
void |
clear()
Removes all elements from this vector, leaving it empty.
|
boolean |
contains(Object object)
Searches this vector for the specified object.
|
boolean |
containsAll(Collection<?> collection)
Searches this vector for all objects in the specified collection.
|
void |
copyInto(Object[] elements)
Attempts to copy elements contained by this
Vector into the
corresponding elements of the supplied Object array. |
E |
elementAt(int location)
Returns the element at the specified location in this vector.
|
Enumeration<E> |
elements()
Returns an enumeration on the elements of this vector.
|
void |
ensureCapacity(int minimumCapacity)
Ensures that this vector can hold the specified number of elements
without growing.
|
boolean |
equals(Object object)
Compares the specified object to this vector and returns if they are
equal.
|
E |
firstElement()
Returns the first element in this vector.
|
E |
get(int location)
Returns the element at the specified location in this vector.
|
int |
hashCode()
Returns an integer hash code for the receiver.
|
int |
indexOf(Object object)
Searches in this vector for the index of the specified object.
|
int |
indexOf(Object object,
int location)
Searches in this vector for the index of the specified object.
|
void |
insertElementAt(E object,
int location)
Inserts the specified object into this vector at the specified location.
|
boolean |
isEmpty()
Returns if this vector has no elements, a size of zero.
|
E |
lastElement()
Returns the last element in this vector.
|
int |
lastIndexOf(Object object)
Searches in this vector for the index of the specified object.
|
int |
lastIndexOf(Object object,
int location)
Searches in this vector for the index of the specified object.
|
E |
remove(int location)
Removes the object at the specified location from this vector.
|
boolean |
remove(Object object)
Removes the first occurrence, starting at the beginning and moving
towards the end, of the specified object from this vector.
|
boolean |
removeAll(Collection<?> collection)
Removes all occurrences in this vector of each object in the specified
Collection.
|
void |
removeAllElements()
Removes all elements from this vector, leaving the size zero and the
capacity unchanged.
|
boolean |
removeElement(Object object)
Removes the first occurrence, starting at the beginning and moving
towards the end, of the specified object from this vector.
|
void |
removeElementAt(int location)
Removes the element found at index position
location from
this Vector . |
protected void |
removeRange(int start,
int end)
Removes the objects in the specified range from the start to the, but not
including, end index.
|
boolean |
retainAll(Collection<?> collection)
Removes all objects from this vector that are not contained in the
specified collection.
|
E |
set(int location,
E object)
Replaces the element at the specified location in this vector with the
specified object.
|
void |
setElementAt(E object,
int location)
Replaces the element at the specified location in this vector with the
specified object.
|
void |
setSize(int length)
Sets the size of this vector to the specified size.
|
int |
size()
Returns the number of elements in this vector.
|
List<E> |
subList(int start,
int end)
Returns a List of the specified portion of this vector from the start
index to one less than the end index.
|
Object[] |
toArray()
Returns a new array containing all elements contained in this vector.
|
<T> T[] |
toArray(T[] contents)
Returns an array containing all elements contained in this vector.
|
String |
toString()
Returns the string representation of this vector.
|
void |
trimToSize()
Sets the capacity of this vector to be the same as the size.
|
iterator, listIterator, listIterator
iterator, listIterator, listIterator
protected int elementCount
protected Object[] elementData
protected int capacityIncrement
public Vector()
public Vector(int capacity)
capacity
- the initial capacity of the new vector.IllegalArgumentException
- if capacity
is negative.public Vector(int capacity, int capacityIncrement)
capacity
- the initial capacity of the new vector.capacityIncrement
- the amount to increase the capacity when this vector is full.IllegalArgumentException
- if capacity
is negative.public Vector(Collection<? extends E> collection)
Vector
containing the elements in
collection
. The order of the elements in the new Vector
is dependent on the iteration order of the seed collection.collection
- the collection of elements to add.public void add(int location, E object)
add
in interface List<E>
add
in class AbstractList<E>
location
- the index at which to insert the element.object
- the object to insert in this vector.ArrayIndexOutOfBoundsException
- if location < 0 || location > size()
.addElement(E)
,
size()
public boolean add(E object)
add
in interface Collection<E>
add
in interface List<E>
add
in class AbstractList<E>
object
- the object to add to the vector.true
public boolean addAll(int location, Collection<? extends E> collection)
location
have their index increased by the size of
the added collection.addAll
in interface List<E>
addAll
in class AbstractList<E>
location
- the location to insert the objects.collection
- the collection of objects.true
if this vector is modified, false
otherwise.ArrayIndexOutOfBoundsException
- if location < 0
or location > size()
.public boolean addAll(Collection<? extends E> collection)
addAll
in interface Collection<E>
addAll
in interface List<E>
addAll
in class AbstractCollection<E>
collection
- the collection of objects.true
if this vector is modified, false
otherwise.public void addElement(E object)
object
- the object to add to the vector.public int capacity()
ensureCapacity(int)
,
size()
public void clear()
public boolean contains(Object object)
contains
in interface Collection<E>
contains
in interface List<E>
contains
in class AbstractCollection<E>
object
- the object to look for in this vector.true
if object is an element of this vector,
false
otherwise.indexOf(Object)
,
indexOf(Object, int)
,
Object.equals(java.lang.Object)
public boolean containsAll(Collection<?> collection)
containsAll
in interface Collection<E>
containsAll
in interface List<E>
containsAll
in class AbstractCollection<E>
collection
- the collection of objects.true
if all objects in the specified collection are
elements of this vector, false
otherwise.public void copyInto(Object[] elements)
Vector
into the
corresponding elements of the supplied Object
array.elements
- the Object
array into which the elements of this
vector are copied.IndexOutOfBoundsException
- if elements
is not big enough.Object.clone()
public E elementAt(int location)
location
- the index of the element to return in this vector.ArrayIndexOutOfBoundsException
- if location < 0 || location >= size()
.size()
public Enumeration<E> elements()
elementAt(int)
,
Enumeration
public void ensureCapacity(int minimumCapacity)
minimumCapacity
- the minimum number of elements that this vector will hold
before growing.capacity()
public boolean equals(Object object)
equals
in interface Collection<E>
equals
in interface List<E>
equals
in class AbstractList<E>
object
- the object to compare with this objecttrue
if the specified object is equal to this vector,
false
otherwise.hashCode()
public E firstElement()
NoSuchElementException
- if this vector is empty.elementAt(int)
,
lastElement()
,
size()
public E get(int location)
get
in interface List<E>
get
in class AbstractList<E>
location
- the index of the element to return in this vector.ArrayIndexOutOfBoundsException
- if location < 0 || location >= size()
.size()
public int hashCode()
hashCode
in interface Collection<E>
hashCode
in interface List<E>
hashCode
in class AbstractList<E>
equals(java.lang.Object)
public int indexOf(Object object)
indexOf
in interface List<E>
indexOf
in class AbstractList<E>
object
- the object to find in this vector.contains(java.lang.Object)
,
lastIndexOf(Object)
,
lastIndexOf(Object, int)
public int indexOf(Object object, int location)
object
- the object to find in this vector.location
- the index at which to start searching.ArrayIndexOutOfBoundsException
- if location < 0
.contains(java.lang.Object)
,
lastIndexOf(Object)
,
lastIndexOf(Object, int)
public void insertElementAt(E object, int location)
location
have their index increased by 1. If the location is
equal to the size of this vector, the object is added at the end.object
- the object to insert in this vector.location
- the index at which to insert the element.ArrayIndexOutOfBoundsException
- if location < 0 || location > size()
.addElement(E)
,
size()
public boolean isEmpty()
isEmpty
in interface Collection<E>
isEmpty
in interface List<E>
isEmpty
in class AbstractCollection<E>
true
if this vector has no elements, false
otherwise.size()
public E lastElement()
NoSuchElementException
- if this vector is empty.elementAt(int)
,
firstElement()
,
size()
public int lastIndexOf(Object object)
lastIndexOf
in interface List<E>
lastIndexOf
in class AbstractList<E>
object
- the object to find in this vector.contains(java.lang.Object)
,
indexOf(Object)
,
indexOf(Object, int)
public int lastIndexOf(Object object, int location)
object
- the object to find in this vector.location
- the index at which to start searching.ArrayIndexOutOfBoundsException
- if location >= size()
.contains(java.lang.Object)
,
indexOf(Object)
,
indexOf(Object, int)
public E remove(int location)
location
have their index
decreased by 1.remove
in interface List<E>
remove
in class AbstractList<E>
location
- the index of the object to remove.IndexOutOfBoundsException
- if location < 0 || location >= size()
.public boolean remove(Object object)
remove
in interface Collection<E>
remove
in interface List<E>
remove
in class AbstractCollection<E>
object
- the object to remove from this vector.true
if the specified object was found, false
otherwise.removeAllElements()
,
removeElementAt(int)
,
size()
public boolean removeAll(Collection<?> collection)
removeAll
in interface Collection<E>
removeAll
in interface List<E>
removeAll
in class AbstractCollection<E>
collection
- the collection of objects to remove.true
if this vector is modified, false
otherwise.remove(Object)
,
contains(Object)
public void removeAllElements()
public boolean removeElement(Object object)
object
- the object to remove from this vector.true
if the specified object was found, false
otherwise.removeAllElements()
,
removeElementAt(int)
,
size()
public void removeElementAt(int location)
location
from
this Vector
. All elements with an index bigger than
location
have their index decreased by 1.location
- the index of the element to remove.ArrayIndexOutOfBoundsException
- if location < 0 || location >= size()
.removeElement(java.lang.Object)
,
removeAllElements()
,
size()
protected void removeRange(int start, int end)
end
have their index decreased by end - start
.removeRange
in class AbstractList<E>
start
- the index at which to start removing.end
- the index one past the end of the range to remove.IndexOutOfBoundsException
- if start < 0, start > end
or
end > size()
.public boolean retainAll(Collection<?> collection)
retainAll
in interface Collection<E>
retainAll
in interface List<E>
retainAll
in class AbstractCollection<E>
collection
- the collection of objects to retain.true
if this vector is modified, false
otherwise.remove(Object)
public E set(int location, E object)
set
in interface List<E>
set
in class AbstractList<E>
location
- the index at which to put the specified object.object
- the object to add to this vector.ArrayIndexOutOfBoundsException
- if location < 0 || location >= size()
.size()
public void setElementAt(E object, int location)
object
- the object to add to this vector.location
- the index at which to put the specified object.ArrayIndexOutOfBoundsException
- if location < 0 || location >= size()
.size()
public void setSize(int length)
length
- the new size of this vector.size()
public int size()
size
in interface Collection<E>
size
in interface List<E>
size
in class AbstractCollection<E>
elementCount
,
lastElement()
public List<E> subList(int start, int end)
subList
in interface List<E>
subList
in class AbstractList<E>
start
- the index at which to start the sublist.end
- the index one past the end of the sublist.IndexOutOfBoundsException
- if start < 0
or end > size()
.IllegalArgumentException
- if start > end
.public Object[] toArray()
toArray
in interface Collection<E>
toArray
in interface List<E>
toArray
in class AbstractList<E>
public <T> T[] toArray(T[] contents)
toArray
in interface Collection<E>
toArray
in interface List<E>
toArray
in class AbstractList<E>
contents
- the array to fill.ArrayStoreException
- if the type of an element in this vector cannot be
stored in the type of the specified array.public String toString()
toString
in class AbstractCollection<E>
elements()
public void trimToSize()
capacity()
,
ensureCapacity(int)
,
size()