Class Vector<E>
- All Implemented Interfaces:
Iterable<E>, Collection<E>, List<E>, RandomAccess
- Direct Known Subclasses:
Stack
Vector is a variable size contiguous indexable array of objects. The size of the vector is the number of objects it contains. The capacity of the vector is the number of objects it can hold.
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).
See also
- java.lang.StringBuffer
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intHow many elements should be added to the vector when it is detected that it needs to grow to accommodate extra entries.protected intThe number of elements or the size of the vector.protected Object[]The elements of the vector.Fields inherited from class AbstractList
modCount -
Constructor Summary
ConstructorsConstructorDescriptionVector()Constructs a new vector using the default capacity.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.Vector(Collection<? extends E> collection) Constructs a new instance ofVectorcontaining the elements incollection. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds the specified object into this vector at the specified location.booleanAdds the specified object at the end of this vector.booleanaddAll(int location, Collection<? extends E> collection) Inserts the objects in the specified collection at the specified location in this vector.booleanaddAll(Collection<? extends E> collection) Adds the objects in the specified collection to the end of this vector.voidaddElement(E object) Adds the specified object at the end of this vector.intcapacity()Returns the number of elements this vector can hold without growing.voidclear()Removes all elements from this vector, leaving it empty.booleanSearches this vector for the specified object.booleancontainsAll(Collection<?> collection) Searches this vector for all objects in the specified collection.voidAttempts to copy elements contained by thisVectorinto the corresponding elements of the suppliedObjectarray.elementAt(int location) Returns the element at the specified location in this vector.elements()Returns an enumeration on the elements of this vector.voidensureCapacity(int minimumCapacity) Ensures that this vector can hold the specified number of elements without growing.booleanCompares the specified object to this vector and returns if they are equal.Returns the first element in this vector.get(int location) Returns the element at the specified location in this vector.inthashCode()Returns an integer hash code for the receiver.intSearches in this vector for the index of the specified object.intSearches in this vector for the index of the specified object.voidinsertElementAt(E object, int location) Inserts the specified object into this vector at the specified location.booleanisEmpty()Returns if this vector has no elements, a size of zero.Returns the last element in this vector.intlastIndexOf(Object object) Searches in this vector for the index of the specified object.intlastIndexOf(Object object, int location) Searches in this vector for the index of the specified object.remove(int location) Removes the object at the specified location from this vector.booleanRemoves the first occurrence, starting at the beginning and moving towards the end, of the specified object from this vector.booleanremoveAll(Collection<?> collection) Removes all occurrences in this vector of each object in the specified Collection.voidRemoves all elements from this vector, leaving the size zero and the capacity unchanged.booleanremoveElement(Object object) Removes the first occurrence, starting at the beginning and moving towards the end, of the specified object from this vector.voidremoveElementAt(int location) Removes the element found at index positionlocationfrom thisVector.protected voidremoveRange(int start, int end) Removes the objects in the specified range from the start to the, but not including, end index.booleanretainAll(Collection<?> collection) Removes all objects from this vector that are not contained in the specified collection.Replaces the element at the specified location in this vector with the specified object.voidsetElementAt(E object, int location) Replaces the element at the specified location in this vector with the specified object.voidsetSize(int length) Sets the size of this vector to the specified size.intsize()Returns the number of elements in this vector.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.toString()Returns the string representation of this vector.voidSets the capacity of this vector to be the same as the size.Methods inherited from class AbstractList
iterator, listIterator, listIteratorMethods inherited from interface List
iterator, listIterator, listIterator
-
Field Details
-
elementCount
protected int elementCountThe number of elements or the size of the vector. -
elementData
The elements of the vector. -
capacityIncrement
protected int capacityIncrementHow many elements should be added to the vector when it is detected that it needs to grow to accommodate extra entries. If this value is zero or negative the size will be doubled if an increase is needed.
-
-
Constructor Details
-
Vector
public Vector()Constructs a new vector using the default capacity. -
Vector
public Vector(int capacity) Constructs a new vector using the specified capacity.
Parameters
capacity: the initial capacity of the new vector.
Throws
IllegalArgumentException: ifcapacityis negative.
-
Vector
public Vector(int capacity, int capacityIncrement) Constructs a new vector using the specified capacity and capacity increment.
Parameters
-
capacity: the initial capacity of the new vector. -
capacityIncrement: the amount to increase the capacity when this vector is full.
Throws
IllegalArgumentException: ifcapacityis negative.
-
-
Vector
Constructs a new instance of
Vectorcontaining the elements incollection. The order of the elements in the newVectoris dependent on the iteration order of the seed collection.Parameters
collection: the collection of elements to add.
-
-
Method Details
-
add
Adds the specified object into this vector at the specified location. The object is inserted before any element with the same or a higher index increasing their index by 1. If the location is equal to the size of this vector, the object is added at the end.
Parameters
-
location: the index at which to insert the element. -
object: the object to insert in this vector.
Throws
ArrayIndexOutOfBoundsException: iflocation size().
See also
-
#addElement
-
#size
-
-
add
Adds the specified object at the end of this vector.
Parameters
object: the object to add to the vector.
Returns
true- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceList<E>- Overrides:
addin classAbstractList<E>- Returns:
trueif thisCollectionis modified,falseotherwise.Throws
-
UnsupportedOperationException: if adding to thisCollectionis not supported. -
ClassCastException: @throws ClassCastException if the class of the object is inappropriate for this collection. -
IllegalArgumentException: if the object cannot be added to thisCollection. -
NullPointerException: if null elements cannot be added to theCollection.
-
-
addAll
Inserts the objects in the specified collection at the specified location in this vector. The objects are inserted in the order in which they are returned from the Collection iterator. The elements with an index equal or higher than
locationhave their index increased by the size of the added collection.Parameters
-
location: the location to insert the objects. -
collection: the collection of objects.
Returns
trueif this vector is modified,falseotherwise.Throws
ArrayIndexOutOfBoundsException: iflocation size().
- Specified by:
addAllin interfaceList<E>- Overrides:
addAllin classAbstractList<E>- Returns:
true if this
Listhas been modified through the insertion, false otherwise (i.e. if the passed collection was empty).Throws
-
UnsupportedOperationException: if adding to thisListis not supported. -
ClassCastException: @throws ClassCastException if the class of an object is inappropriate for thisList. -
IllegalArgumentException: if an object cannot be added to thisList. -
IndexOutOfBoundsException: iflocation size()
-
-
-
addAll
Adds the objects in the specified collection to the end of this vector.
Parameters
collection: the collection of objects.
Returns
trueif this vector is modified,falseotherwise.- Specified by:
addAllin interfaceCollection<E>- Specified by:
addAllin interfaceList<E>- Overrides:
addAllin classAbstractCollection<E>- Returns:
trueif thisCollectionis modified,falseotherwise.Throws
-
UnsupportedOperationException: if adding to thisCollectionis not supported. -
ClassCastException: @throws ClassCastException if the class of an object is inappropriate for thisCollection. -
IllegalArgumentException: if an object cannot be added to thisCollection. -
NullPointerException: @throws NullPointerException ifcollectionisnull, or if it containsnullelements and thisCollectiondoes not support such elements.
-
-
addElement
Adds the specified object at the end of this vector.
Parameters
object: the object to add to the vector.
-
capacity
public int capacity()Returns the number of elements this vector can hold without growing.
Returns
the capacity of this vector.
See also
-
#ensureCapacity
-
#size
-
-
clear
public void clear()Removes all elements from this vector, leaving it empty.
See also
-
#isEmpty
-
#size
- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceList<E>- Overrides:
clearin classAbstractList<E>
-
-
contains
Searches this vector for the specified object.
Parameters
object: the object to look for in this vector.
Returns
- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceList<E>- Overrides:
containsin classAbstractCollection<E>- Returns:
trueif object is an element of this vector,falseotherwise.See also
-
#indexOf(Object)
-
#indexOf(Object, int)
-
java.lang.Object#equals
-
-
containsAll
Searches this vector for all objects in the specified collection.
Parameters
collection: the collection of objects.
Returns
- Specified by:
containsAllin interfaceCollection<E>- Specified by:
containsAllin interfaceList<E>- Overrides:
containsAllin classAbstractCollection<E>- Returns:
trueif all objects in the specified collection are elements of this vector,falseotherwise.
-
copyInto
Attempts to copy elements contained by this
Vectorinto the corresponding elements of the suppliedObjectarray.Parameters
elements: @param elements theObjectarray into which the elements of this vector are copied.
Throws
IndexOutOfBoundsException: ifelementsis not big enough.
See also
- #clone
-
elementAt
Returns the element at the specified location in this vector.
Parameters
location: the index of the element to return in this vector.
Returns
the element at the specified location.
Throws
ArrayIndexOutOfBoundsException: iflocation = size().
See also
- #size
-
elements
Returns an enumeration on the elements of this vector. The results of the enumeration may be affected if the contents of this vector is modified.
Returns
an enumeration of the elements of this vector.
See also
-
#elementAt
-
Enumeration
-
-
ensureCapacity
public void ensureCapacity(int minimumCapacity) Ensures that this vector can hold the specified number of elements without growing.
Parameters
minimumCapacity: @param minimumCapacity the minimum number of elements that this vector will hold before growing.
See also
- #capacity
-
equals
Compares the specified object to this vector and returns if they are equal. The object must be a List which contains the same objects in the same order.
Parameters
object: the object to compare with this object
Returns
- Specified by:
equalsin interfaceCollection<E>- Specified by:
equalsin interfaceList<E>- Overrides:
equalsin classAbstractList<E>- Returns:
trueif the specified object is equal to this vector,falseotherwise.See also
- #hashCode
-
firstElement
Returns the first element in this vector.
Returns
the element at the first position.
Throws
NoSuchElementException: if this vector is empty.
See also
-
#elementAt
-
#lastElement
-
#size
-
get
Returns the element at the specified location in this vector.
Parameters
location: the index of the element to return in this vector.
Returns
the element at the specified location.
Throws
ArrayIndexOutOfBoundsException: iflocation = size().
See also
- #size
-
hashCode
public int hashCode()Returns an integer hash code for the receiver. Objects which are equal return the same value for this method.
Returns
the receiver's hash.
See also
- #equals
- Specified by:
hashCodein interfaceCollection<E>- Specified by:
hashCodein interfaceList<E>- Overrides:
hashCodein classAbstractList<E>
-
indexOf
Searches in this vector for the index of the specified object. The search for the object starts at the beginning and moves towards the end of this vector.
Parameters
object: the object to find in this vector.
Returns
-
indexOf
Searches in this vector for the index of the specified object. The search for the object starts at the specified location and moves towards the end of this vector.
Parameters
-
object: the object to find in this vector. -
location: the index at which to start searching.
Returns
- Returns:
the index in this vector of the specified element, -1 if the element isn't found.
Throws
ArrayIndexOutOfBoundsException: iflocation < 0.
See also
-
#contains
-
#lastIndexOf(Object)
-
#lastIndexOf(Object, int)
-
-
insertElementAt
Inserts the specified object into this vector at the specified location. This object is inserted before any previous element at the specified location. All elements with an index equal or greater than
locationhave their index increased by 1. If the location is equal to the size of this vector, the object is added at the end.Parameters
-
object: the object to insert in this vector. -
location: the index at which to insert the element.
Throws
ArrayIndexOutOfBoundsException: iflocation size().
See also
-
#addElement
-
#size
-
-
isEmpty
public boolean isEmpty()Returns if this vector has no elements, a size of zero.
Returns
- Specified by:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceList<E>- Overrides:
isEmptyin classAbstractCollection<E>- Returns:
trueif this vector has no elements,falseotherwise.See also
- #size
-
lastElement
Returns the last element in this vector.
Returns
the element at the last position.
Throws
NoSuchElementException: if this vector is empty.
See also
-
#elementAt
-
#firstElement
-
#size
-
lastIndexOf
Searches in this vector for the index of the specified object. The search for the object starts at the end and moves towards the start of this vector.
Parameters
object: the object to find in this vector.
Returns
- Specified by:
lastIndexOfin interfaceList<E>- Overrides:
lastIndexOfin classAbstractList<E>- Returns:
the index in this vector of the specified element, -1 if the element isn't found.
See also
-
#contains
-
#indexOf(Object)
-
#indexOf(Object, int)
-
-
lastIndexOf
Searches in this vector for the index of the specified object. The search for the object starts at the specified location and moves towards the start of this vector.
Parameters
-
object: the object to find in this vector. -
location: the index at which to start searching.
Returns
- Returns:
the index in this vector of the specified element, -1 if the element isn't found.
Throws
ArrayIndexOutOfBoundsException: iflocation >= size().
See also
-
#contains
-
#indexOf(Object)
-
#indexOf(Object, int)
-
-
remove
Removes the object at the specified location from this vector. All elements with an index bigger than
locationhave their index decreased by 1.Parameters
location: the index of the object to remove.
Returns
the removed object.
Throws
IndexOutOfBoundsException: iflocation = size().
-
remove
Removes the first occurrence, starting at the beginning and moving towards the end, of the specified object from this vector. All elements with an index bigger than the element that gets removed have their index decreased by 1.
Parameters
object: the object to remove from this vector.
Returns
- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceList<E>- Overrides:
removein classAbstractCollection<E>- Returns:
trueif the specified object was found,falseotherwise.See also
-
#removeAllElements
-
#removeElementAt
-
#size
-
-
removeAll
Removes all occurrences in this vector of each object in the specified Collection.
Parameters
collection: the collection of objects to remove.
Returns
trueif this vector is modified,falseotherwise.See also
-
#remove(Object)
-
#contains(Object)
- Specified by:
removeAllin interfaceCollection<E>- Specified by:
removeAllin interfaceList<E>- Overrides:
removeAllin classAbstractCollection<E>- Returns:
trueif thisCollectionis modified,falseotherwise.Throws
-
UnsupportedOperationException: if removing from thisCollectionis not supported. -
ClassCastException: @throws ClassCastException if one or more elements ofcollectionisn't of the correct type. -
NullPointerException: @throws NullPointerException ifcollectioncontains at least onenullelement and thisCollectiondoesn't supportnullelements. -
NullPointerException: ifcollectionisnull.
-
-
removeAllElements
public void removeAllElements()Removes all elements from this vector, leaving the size zero and the capacity unchanged.
See also
-
#isEmpty
-
#size
-
-
removeElement
Removes the first occurrence, starting at the beginning and moving towards the end, of the specified object from this vector. All elements with an index bigger than the element that gets removed have their index decreased by 1.
Parameters
object: the object to remove from this vector.
Returns
- Returns:
trueif the specified object was found,falseotherwise.See also
-
#removeAllElements
-
#removeElementAt
-
#size
-
-
removeElementAt
public void removeElementAt(int location) Removes the element found at index position
locationfrom thisVector. All elements with an index bigger thanlocationhave their index decreased by 1.Parameters
location: the index of the element to remove.
Throws
ArrayIndexOutOfBoundsException: iflocation = size().
See also
-
#removeElement
-
#removeAllElements
-
#size
-
removeRange
protected void removeRange(int start, int end) Removes the objects in the specified range from the start to the, but not including, end index. All elements with an index bigger than or equal to
endhave their index decreased byend - start.Parameters
-
start: the index at which to start removing. -
end: the index one past the end of the range to remove.
Throws
IndexOutOfBoundsException: @throws IndexOutOfBoundsException ifstart endorend > size().
- Overrides:
removeRangein classAbstractList<E>
-
-
retainAll
Removes all objects from this vector that are not contained in the specified collection.
Parameters
collection: the collection of objects to retain.
Returns
trueif this vector is modified,falseotherwise.See also
- #remove(Object)
- Specified by:
retainAllin interfaceCollection<E>- Specified by:
retainAllin interfaceList<E>- Overrides:
retainAllin classAbstractCollection<E>- Returns:
trueif thisCollectionis modified,falseotherwise.Throws
-
UnsupportedOperationException: if removing from thisCollectionis not supported. -
ClassCastException: @throws ClassCastException if one or more elements ofcollectionisn't of the correct type. -
NullPointerException: @throws NullPointerException ifcollectioncontains at least onenullelement and thisCollectiondoesn't supportnullelements. -
NullPointerException: ifcollectionisnull.
-
-
set
Replaces the element at the specified location in this vector with the specified object.
Parameters
-
location: the index at which to put the specified object. -
object: the object to add to this vector.
Returns
the previous element at the location.
Throws
ArrayIndexOutOfBoundsException: iflocation = size().
See also
- #size
-
-
setElementAt
Replaces the element at the specified location in this vector with the specified object.
Parameters
-
object: the object to add to this vector. -
location: the index at which to put the specified object.
Throws
ArrayIndexOutOfBoundsException: iflocation = size().
See also
- #size
-
-
setSize
public void setSize(int length) Sets the size of this vector to the specified size. If there are more than length elements in this vector, the elements at end are lost. If there are less than length elements in the vector, the additional elements contain null.
Parameters
length: the new size of this vector.
See also
- #size
-
size
public int size()Returns the number of elements in this vector.
Returns
the number of elements in this vector.
See also
-
#elementCount
-
#lastElement
- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceList<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- how many objects this
Collectioncontains, orInteger.MAX_VALUEif there are more thanInteger.MAX_VALUEelements in thisCollection.
-
-
subList
Returns a List of the specified portion of this vector from the start index to one less than the end index. The returned List is backed by this vector so changes to one are reflected by the other.
Parameters
-
start: the index at which to start the sublist. -
end: the index one past the end of the sublist.
Returns
a List of a portion of this vector.
Throws
-
IndexOutOfBoundsException: ifstart size(). -
IllegalArgumentException: ifstart > end.
-
-
toArray
Returns a new array containing all elements contained in this vector.
Returns
an array of the elements from this vector.
- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceList<E>- Overrides:
toArrayin classAbstractList<E>
-
toArray
public <T> T[] toArray(T[] contents) Returns an array containing all elements contained in this vector. If the specified array is large enough to hold the elements, the specified array is used, otherwise an array of the same type is created. If the specified array is used and is larger than this vector, the array element following the collection elements is set to null.
Parameters
contents: the array to fill.
Returns
an array of the elements from this vector.
Throws
ArrayStoreException: @throws ArrayStoreException if the type of an element in this vector cannot be stored in the type of the specified array.
- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceList<E>- Overrides:
toArrayin classAbstractList<E>
-
toString
Returns the string representation of this vector.
Returns
the string representation of this vector.
See also
- #elements
- Overrides:
toStringin classAbstractCollection<E>
-
trimToSize
public void trimToSize()Sets the capacity of this vector to be the same as the size.
See also
-
#capacity
-
#ensureCapacity
-
#size
-
-