Skip to content

Most visited

Recently visited

navigation
Added in API level 1

Vector

public class Vector
extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable

java.lang.Object
   ↳ java.util.AbstractCollection<E>
     ↳ java.util.AbstractList<E>
       ↳ java.util.Vector<E>
Known Direct Subclasses


Vector is an implementation of List, backed by an array and synchronized. All optional operations including adding, removing, and replacing elements are supported.

All elements are permitted, including null.

This class is equivalent to ArrayList with synchronized operations. This has a performance cost, and the synchronization is not necessarily meaningful to your application: synchronizing each call to get, for example, is not equivalent to synchronizing on the list and iterating over it (which is probably what you intended). If you do need very highly concurrent access, you should also consider CopyOnWriteArrayList.

Summary

Fields

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.

Inherited fields

From class java.util.AbstractList

Public constructors

Vector()

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 of Vector containing the elements in collection.

Public methods

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.

Object clone()

Returns a new vector with the same elements, size, capacity and capacity increment as this vector.

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, int location)

Searches in this vector for the index of the specified object.

int indexOf(Object object)

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, int location)

Searches in this vector for the index of the specified object.

int lastIndexOf(Object object)

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.

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.

<T> T[] toArray(T[] contents)

Returns an array containing all elements contained in this vector.

Object[] toArray()

Returns a new 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.

Protected methods

void removeRange(int start, int end)

Removes the objects in the specified range from the start to the, but not including, end index.

Inherited methods

From class java.util.AbstractList
From class java.util.AbstractCollection
From class java.lang.Object
From interface java.util.List
From interface java.util.Collection
From interface java.lang.Iterable

Fields

capacityIncrement

Added in API level 1
int capacityIncrement

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

elementCount

Added in API level 1
int elementCount

The number of elements or the size of the vector.

elementData

Added in API level 1
Object[] elementData

The elements of the vector.

Public constructors

Vector

Added in API level 1
Vector ()

Constructs a new vector using the default capacity.

Vector

Added in API level 1
Vector (int capacity)

Constructs a new vector using the specified capacity.

Parameters
capacity int: the initial capacity of the new vector.
Throws
IllegalArgumentException if capacity is negative.

Vector

Added in API level 1
Vector (int capacity, 
                int capacityIncrement)

Constructs a new vector using the specified capacity and capacity increment.

Parameters
capacity int: the initial capacity of the new vector.
capacityIncrement int: the amount to increase the capacity when this vector is full.
Throws
IllegalArgumentException if capacity is negative.

Vector

Added in API level 1
Vector (Collection<? extends E> collection)

Constructs a new instance of 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.

Parameters
collection Collection: the collection of elements to add.

Public methods

add

Added in API level 1
boolean add (E object)

Adds the specified object at the end of this vector.

Parameters
object E: the object to add to the vector.
Returns
boolean true

add

Added in API level 1
void add (int location, 
                E object)

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 int: the index at which to insert the element.
object E: the object to insert in this vector.
Throws
ArrayIndexOutOfBoundsException if location < 0 || location > size().

See also:

addAll

Added in API level 1
boolean addAll (Collection<? extends E> collection)

Adds the objects in the specified collection to the end of this vector.

Parameters
collection Collection: the collection of objects.
Returns
boolean true if this vector is modified, false otherwise.

addAll

Added in API level 1
boolean addAll (int location, 
                Collection<? extends E> collection)

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 location have their index increased by the size of the added collection.

Parameters
location int: the location to insert the objects.
collection Collection: the collection of objects.
Returns
boolean true if this vector is modified, false otherwise.
Throws
ArrayIndexOutOfBoundsException if location < 0 or location > size().

addElement

Added in API level 1
void addElement (E object)

Adds the specified object at the end of this vector.

Parameters
object E: the object to add to the vector.

capacity

Added in API level 1
int capacity ()

Returns the number of elements this vector can hold without growing.

Returns
int the capacity of this vector.

See also:

clear

Added in API level 1
void clear ()

Removes all elements from this vector, leaving it empty.

See also:

clone

Added in API level 1
Object clone ()

Returns a new vector with the same elements, size, capacity and capacity increment as this vector.

Returns
Object a shallow copy of this vector.

See also:

contains

Added in API level 1
boolean contains (Object object)

Searches this vector for the specified object.

Parameters
object Object: the object to look for in this vector.
Returns
boolean true if object is an element of this vector, false otherwise.

See also:

containsAll

Added in API level 1
boolean containsAll (Collection<?> collection)

Searches this vector for all objects in the specified collection.

Parameters
collection Collection: the collection of objects.
Returns
boolean true if all objects in the specified collection are elements of this vector, false otherwise.

copyInto

Added in API level 1
void copyInto (Object[] elements)

Attempts to copy elements contained by this Vector into the corresponding elements of the supplied Object array.

Parameters
elements Object: the Object array into which the elements of this vector are copied.
Throws
IndexOutOfBoundsException if elements is not big enough.

See also:

elementAt

Added in API level 1
E elementAt (int location)

Returns the element at the specified location in this vector.

Parameters
location int: the index of the element to return in this vector.
Returns
E the element at the specified location.
Throws
ArrayIndexOutOfBoundsException if location < 0 || location >= size().

See also:

elements

Added in API level 1
Enumeration<E> 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
Enumeration<E> an enumeration of the elements of this vector.

See also:

ensureCapacity

Added in API level 1
void ensureCapacity (int minimumCapacity)

Ensures that this vector can hold the specified number of elements without growing.

Parameters
minimumCapacity int: the minimum number of elements that this vector will hold before growing.

See also:

equals

Added in API level 1
boolean equals (Object object)

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 Object: the object to compare with this object
Returns
boolean true if the specified object is equal to this vector, false otherwise.

See also:

firstElement

Added in API level 1
E firstElement ()

Returns the first element in this vector.

Returns
E the element at the first position.
Throws
NoSuchElementException if this vector is empty.

See also:

get

Added in API level 1
E get (int location)

Returns the element at the specified location in this vector.

Parameters
location int: the index of the element to return in this vector.
Returns
E the element at the specified location.
Throws
ArrayIndexOutOfBoundsException if location < 0 || location >= size().

See also:

hashCode

Added in API level 1
int hashCode ()

Returns an integer hash code for the receiver. Objects which are equal return the same value for this method.

Returns
int the receiver's hash.

See also:

indexOf

Added in API level 1
int indexOf (Object object, 
                int location)

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 Object: the object to find in this vector.
location int: the index at which to start searching.
Returns
int the index in this vector of the specified element, -1 if the element isn't found.
Throws
ArrayIndexOutOfBoundsException if location < 0.

See also:

indexOf

Added in API level 1
int indexOf (Object object)

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 Object: the object to find in this vector.
Returns
int the index in this vector of the specified element, -1 if the element isn't found.

See also:

insertElementAt

Added in API level 1
void insertElementAt (E object, 
                int location)

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

Parameters
object E: the object to insert in this vector.
location int: the index at which to insert the element.
Throws
ArrayIndexOutOfBoundsException if location < 0 || location > size().

See also:

isEmpty

Added in API level 1
boolean isEmpty ()

Returns if this vector has no elements, a size of zero.

Returns
boolean true if this vector has no elements, false otherwise.

See also:

lastElement

Added in API level 1
E lastElement ()

Returns the last element in this vector.

Returns
E the element at the last position.
Throws
NoSuchElementException if this vector is empty.

See also:

lastIndexOf

Added in API level 1
int lastIndexOf (Object object, 
                int location)

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 Object: the object to find in this vector.
location int: the index at which to start searching.
Returns
int the index in this vector of the specified element, -1 if the element isn't found.
Throws
ArrayIndexOutOfBoundsException if location >= size().

See also:

lastIndexOf

Added in API level 1
int lastIndexOf (Object object)

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 Object: the object to find in this vector.
Returns
int the index in this vector of the specified element, -1 if the element isn't found.

See also:

remove

Added in API level 1
E remove (int location)

Removes the object at the specified location from this vector. All elements with an index bigger than location have their index decreased by 1.

Parameters
location int: the index of the object to remove.
Returns
E the removed object.
Throws
IndexOutOfBoundsException if location < 0 || location >= size().

remove

Added in API level 1
boolean remove (Object object)

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 Object: the object to remove from this vector.
Returns
boolean true if the specified object was found, false otherwise.

See also:

removeAll

Added in API level 1
boolean removeAll (Collection<?> collection)

Removes all occurrences in this vector of each object in the specified Collection.

Parameters
collection Collection: the collection of objects to remove.
Returns
boolean true if this vector is modified, false otherwise.

See also:

removeAllElements

Added in API level 1
void removeAllElements ()

Removes all elements from this vector, leaving the size zero and the capacity unchanged.

See also:

removeElement

Added in API level 1
boolean removeElement (Object object)

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 Object: the object to remove from this vector.
Returns
boolean true if the specified object was found, false otherwise.

See also:

removeElementAt

Added in API level 1
void removeElementAt (int location)

Removes the element found at index position location from this Vector. All elements with an index bigger than location have their index decreased by 1.

Parameters
location int: the index of the element to remove.
Throws
ArrayIndexOutOfBoundsException if location < 0 || location >= size().

See also:

retainAll

Added in API level 1
boolean retainAll (Collection<?> collection)

Removes all objects from this vector that are not contained in the specified collection.

Parameters
collection Collection: the collection of objects to retain.
Returns
boolean true if this vector is modified, false otherwise.

See also:

set

Added in API level 1
E set (int location, 
                E object)

Replaces the element at the specified location in this vector with the specified object.

Parameters
location int: the index at which to put the specified object.
object E: the object to add to this vector.
Returns
E the previous element at the location.
Throws
ArrayIndexOutOfBoundsException if location < 0 || location >= size().

See also:

setElementAt

Added in API level 1
void setElementAt (E object, 
                int location)

Replaces the element at the specified location in this vector with the specified object.

Parameters
object E: the object to add to this vector.
location int: the index at which to put the specified object.
Throws
ArrayIndexOutOfBoundsException if location < 0 || location >= size().

See also:

setSize

Added in API level 1
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 int: the new size of this vector.

See also:

size

Added in API level 1
int size ()

Returns the number of elements in this vector.

Returns
int the number of elements in this vector.

See also:

subList

Added in API level 1
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. The returned List is backed by this vector so changes to one are reflected by the other.

Parameters
start int: the index at which to start the sublist.
end int: the index one past the end of the sublist.
Returns
List<E> a List of a portion of this vector.
Throws
IndexOutOfBoundsException if start < 0 or end > size().
IllegalArgumentException if start > end.

toArray

Added in API level 1
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 T: the array to fill.
Returns
T[] an array of the elements from this vector.
Throws
ArrayStoreException if the type of an element in this vector cannot be stored in the type of the specified array.

toArray

Added in API level 1
Object[] toArray ()

Returns a new array containing all elements contained in this vector.

Returns
Object[] an array of the elements from this vector.

toString

Added in API level 1
String toString ()

Returns the string representation of this vector.

Returns
String the string representation of this vector.

See also:

trimToSize

Added in API level 1
void trimToSize ()

Sets the capacity of this vector to be the same as the size.

See also:

Protected methods

removeRange

Added in API level 1
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 end have their index decreased by end - start.

Parameters
start int: the index at which to start removing.
end int: the index one past the end of the range to remove.
Throws
IndexOutOfBoundsException if start < 0, start > end or end > size().
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.