FileDocCategorySizeDatePackage
Vector.javaAPI DocAndroid 1.5 API35611Wed May 06 22:41:04 BST 2009java.util

Vector

public class Vector extends AbstractList implements Serializable, RandomAccess, List, Cloneable
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
java.lang.StringBuffer
since
Android 1.0

Fields Summary
private static final long
serialVersionUID
protected int
elementCount
The number of elements or the size of the vector.
protected Object[]
elementData
The elements of the vector.
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. If this value is zero or negative the size will be doubled if an increase is needed.
private static final int
DEFAULT_SIZE
Constructors Summary
public Vector()
Constructs a new vector using the default capacity.

since
Android 1.0


                     
      
        this(DEFAULT_SIZE, 0);
    
public Vector(int capacity)
Constructs a new vector using the specified capacity.

param
capacity the initial capacity of the new vector.
throws
IllegalArgumentException if {@code capacity} is negative.
since
Android 1.0

        this(capacity, 0);
    
public Vector(int capacity, int capacityIncrement)
Constructs a new vector using the specified capacity and capacity increment.

param
capacity the initial capacity of the new vector.
param
capacityIncrement the amount to increase the capacity when this vector is full.
throws
IllegalArgumentException if {@code capacity} is negative.
since
Android 1.0

        elementCount = 0;
        try {
            elementData = newElementArray(capacity);
        } catch (NegativeArraySizeException e) {
            throw new IllegalArgumentException();
        }
        this.capacityIncrement = capacityIncrement;
    
public Vector(Collection collection)
Constructs a new instance of {@code Vector} containing the elements in {@code collection}. The order of the elements in the new {@code Vector} is dependent on the iteration order of the seed collection.

param
collection the collection of elements to add.
since
Android 1.0

        this(collection.size(), 0);
        Iterator<? extends E> it = collection.iterator();
        while (it.hasNext()) {
            elementData[elementCount++] = it.next();
        }
    
Methods Summary
public voidadd(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.

param
location the index at which to insert the element.
param
object the object to insert in this vector.
exception
ArrayIndexOutOfBoundsException when {@code location < 0 || location > size()}.
see
#addElement
see
#size
since
Android 1.0

        insertElementAt(object, location);
    
public booleanadd(E object)
Adds the specified object at the end of this vector.

param
object the object to add to the vector.
return
{@code true}
since
Android 1.0

        addElement(object);
        return true;
    
public synchronized booleanaddAll(int location, java.util.Collection 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 {@code location} have their index increased by the size of the added collection.

param
location the location to insert the objects.
param
collection the collection of objects.
return
{@code true} if this vector is modified, {@code false} otherwise.
exception
ArrayIndexOutOfBoundsException when {@code location < 0} or {@code location > size()}.
since
Android 1.0

        if (0 <= location && location <= elementCount) {
            int size = collection.size();
            if (size == 0) {
                return false;
            }
            int required = size - (elementData.length - elementCount);
            if (required > 0) {
                growBy(required);
            }
            int count = elementCount - location;
            if (count > 0) {
                System.arraycopy(elementData, location, elementData, location
                        + size, count);
            }
            Iterator<? extends E> it = collection.iterator();
            while (it.hasNext()) {
                elementData[location++] = it.next();
            }
            elementCount += size;
            modCount++;
            return true;
        }
        throw new ArrayIndexOutOfBoundsException(location);
    
public synchronized booleanaddAll(java.util.Collection collection)
Adds the objects in the specified collection to the end of this vector.

param
collection the collection of objects.
return
{@code true} if this vector is modified, {@code false} otherwise.
since
Android 1.0

        return addAll(elementCount, collection);
    
public synchronized voidaddElement(E object)
Adds the specified object at the end of this vector.

param
object the object to add to the vector.
since
Android 1.0

        if (elementCount == elementData.length) {
            growByOne();
        }
        elementData[elementCount++] = object;
        modCount++;
    
public synchronized intcapacity()
Returns the number of elements this vector can hold without growing.

return
the capacity of this vector.
see
#ensureCapacity
see
#size
since
Android 1.0

        return elementData.length;
    
public voidclear()
Removes all elements from this vector, leaving it empty.

see
#isEmpty
see
#size
since
Android 1.0

        removeAllElements();
    
public synchronized java.lang.Objectclone()
Returns a new vector with the same elements, size, capacity and capacity increment as this vector.

return
a shallow copy of this vector.
see
java.lang.Cloneable
since
Android 1.0

        try {
            Vector<E> vector = (Vector<E>) super.clone();
            vector.elementData = elementData.clone();
            return vector;
        } catch (CloneNotSupportedException e) {
            return null;
        }
    
public booleancontains(java.lang.Object object)
Searches this vector for the specified object.

param
object the object to look for in this vector.
return
{@code true} if object is an element of this vector, {@code false} otherwise.
see
#indexOf(Object)
see
#indexOf(Object, int)
see
java.lang.Object#equals
since
Android 1.0

        return indexOf(object, 0) != -1;
    
public synchronized booleancontainsAll(java.util.Collection collection)
Searches this vector for all objects in the specified collection.

param
collection the collection of objects.
return
{@code true} if all objects in the specified collection are elements of this vector, {@code false} otherwise.
since
Android 1.0

        return super.containsAll(collection);
    
public synchronized voidcopyInto(java.lang.Object[] elements)
Attempts to copy elements contained by this {@code Vector} into the corresponding elements of the supplied {@code Object} array.

param
elements the {@code Object} array into which the elements of this vector are copied.
throws
IndexOutOfBoundsException if {@code elements} is not big enough.
see
#clone
since
Android 1.0

        System.arraycopy(elementData, 0, elements, 0, elementCount);
    
public synchronized EelementAt(int location)
Returns the element at the specified location in this vector.

param
location the index of the element to return in this vector.
return
the element at the specified location.
exception
ArrayIndexOutOfBoundsException when {@code location < 0 || location >= size()}.
see
#size
since
Android 1.0

        if (location < elementCount) {
            return (E)elementData[location];
        }
        throw new ArrayIndexOutOfBoundsException(location);
    
public java.util.Enumerationelements()
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.

return
an enumeration of the elements of this vector.
see
#elementAt
see
Enumeration
since
Android 1.0

        return new Enumeration<E>() {
            int pos = 0;

            public boolean hasMoreElements() {
                synchronized (Vector.this) {
                    return pos < elementCount;
                }
            }

            public E nextElement() {
                synchronized (Vector.this) {
                    if (pos < elementCount) {
                        return (E)elementData[pos++];
                    }
                }
                throw new NoSuchElementException();
            }
        };
    
public synchronized voidensureCapacity(int minimumCapacity)
Ensures that this vector can hold the specified number of elements without growing.

param
minimumCapacity the minimum number of elements that this vector will hold before growing.
see
#capacity
since
Android 1.0

        if (elementData.length < minimumCapacity) {
            int next = (capacityIncrement <= 0 ? elementData.length
                    : capacityIncrement)
                    + elementData.length;
            grow(minimumCapacity > next ? minimumCapacity : next);
        }
    
public synchronized booleanequals(java.lang.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.

param
object the object to compare with this object
return
{@code true} if the specified object is equal to this vector, {@code false} otherwise.
see
#hashCode
since
Android 1.0

        if (this == object) {
            return true;
        }
        if (object instanceof List) {
            List<?> list = (List) object;
            if (list.size() != size()) {
                return false;
            }

            int index = 0;
            Iterator<?> it = list.iterator();
            while (it.hasNext()) {
                Object e1 = elementData[index++], e2 = it.next();
                if (!(e1 == null ? e2 == null : e1.equals(e2))) {
                    return false;
                }
            }
            return true;
        }
        return false;
    
public synchronized EfirstElement()
Returns the first element in this vector.

return
the element at the first position.
exception
NoSuchElementException when this vector is empty.
see
#elementAt
see
#lastElement
see
#size
since
Android 1.0

        if (elementCount > 0) {
            return (E)elementData[0];
        }
        throw new NoSuchElementException();
    
public synchronized Eget(int location)
Returns the element at the specified location in this vector.

param
location the index of the element to return in this vector.
return
the element at the specified location.
exception
ArrayIndexOutOfBoundsException when {@code location < 0 || location >= size()}.
see
#size
since
Android 1.0

        return elementAt(location);
    
private voidgrow(int newCapacity)

        E[] newData = newElementArray(newCapacity);
        // Assumes elementCount is <= newCapacity
        assert elementCount <= newCapacity;
        System.arraycopy(elementData, 0, newData, 0, elementCount); 
        elementData = newData;
    
private voidgrowBy(int required)

        int adding = 0;
        if (capacityIncrement <= 0) {
            if ((adding = elementData.length) == 0) {
                adding = required;
            }
            while (adding < required) {
                adding += adding;
            }
        } else {
            adding = (required / capacityIncrement) * capacityIncrement;
            if (adding < required) {
                adding += capacityIncrement;
            }
        }
        E[] newData = newElementArray(elementData.length + adding);
        System.arraycopy(elementData, 0, newData, 0, elementCount);
        elementData = newData;
    
private voidgrowByOne()
JIT optimization

        int adding = 0;
        if (capacityIncrement <= 0) {
            if ((adding = elementData.length) == 0) {
                adding = 1;
            }
        } else {
            adding = capacityIncrement;
        }

        E[] newData = newElementArray(elementData.length + adding);
        System.arraycopy(elementData, 0, newData, 0, elementCount);
        elementData = newData;
    
public synchronized inthashCode()
Returns an integer hash code for the receiver. Objects which are equal return the same value for this method.

return
the receiver's hash.
see
#equals
since
Android 1.0

        int result = 1;
        for (int i = 0; i < elementCount; i++) {
            result = (31 * result)
                    + (elementData[i] == null ? 0 : elementData[i].hashCode());
        }
        return result;
    
public intindexOf(java.lang.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.

param
object the object to find in this vector.
return
the index in this vector of the specified element, -1 if the element isn't found.
see
#contains
see
#lastIndexOf(Object)
see
#lastIndexOf(Object, int)
since
Android 1.0

        return indexOf(object, 0);
    
public synchronized intindexOf(java.lang.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.

param
object the object to find in this vector.
param
location the index at which to start searching.
return
the index in this vector of the specified element, -1 if the element isn't found.
exception
ArrayIndexOutOfBoundsException when {@code location < 0}.
see
#contains
see
#lastIndexOf(Object)
see
#lastIndexOf(Object, int)
since
Android 1.0

        if (object != null) {
            for (int i = location; i < elementCount; i++) {
                if (object.equals(elementData[i])) {
                    return i;
                }
            }
        } else {
            for (int i = location; i < elementCount; i++) {
                if (elementData[i] == null) {
                    return i;
                }
            }
        }
        return -1;
    
public synchronized voidinsertElementAt(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 {@code 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.

param
object the object to insert in this vector.
param
location the index at which to insert the element.
exception
ArrayIndexOutOfBoundsException when {@code location < 0 || location > size()}.
see
#addElement
see
#size
since
Android 1.0

        if (0 <= location && location <= elementCount) {
            if (elementCount == elementData.length) {
                growByOne();
            }
            int count = elementCount - location;
            if (count > 0) {
                System.arraycopy(elementData, location, elementData,
                        location + 1, count);
            }
            elementData[location] = object;
            elementCount++;
            modCount++;
        } else {
            throw new ArrayIndexOutOfBoundsException(location);
        }
    
public synchronized booleanisEmpty()
Returns if this vector has no elements, a size of zero.

return
{@code true} if this vector has no elements, {@code false} otherwise.
see
#size
since
Android 1.0

        return elementCount == 0;
    
public synchronized ElastElement()
Returns the last element in this vector.

return
the element at the last position.
exception
NoSuchElementException when this vector is empty.
see
#elementAt
see
#firstElement
see
#size
since
Android 1.0

        try {
            return (E)elementData[elementCount - 1];
        } catch (IndexOutOfBoundsException e) {
            throw new NoSuchElementException();
        }
    
public synchronized intlastIndexOf(java.lang.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.

param
object the object to find in this vector.
return
the index in this vector of the specified element, -1 if the element isn't found.
see
#contains
see
#indexOf(Object)
see
#indexOf(Object, int)
since
Android 1.0

        return lastIndexOf(object, elementCount - 1);
    
public synchronized intlastIndexOf(java.lang.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.

param
object the object to find in this vector.
param
location the index at which to start searching.
return
the index in this vector of the specified element, -1 if the element isn't found.
exception
ArrayIndexOutOfBoundsException when {@code location >= size()}.
see
#contains
see
#indexOf(Object)
see
#indexOf(Object, int)
since
Android 1.0

        if (location < elementCount) {
            if (object != null) {
                for (int i = location; i >= 0; i--) {
                    if (object.equals(elementData[i])) {
                        return i;
                    }
                }
            } else {
                for (int i = location; i >= 0; i--) {
                    if (elementData[i] == null) {
                        return i;
                    }
                }
            }
            return -1;
        }
        throw new ArrayIndexOutOfBoundsException(location);
    
private E[]newElementArray(int size)

        return (E[])new Object[size];
    
public synchronized Eremove(int location)
Removes the object at the specified location from this vector. All elements with an index bigger than {@code location} have their index decreased by 1.

param
location the index of the object to remove.
return
the removed object.
exception
IndexOutOfBoundsException when {@code location < 0 || location >= size()}.
since
Android 1.0

        if (location < elementCount) {
            E result = (E)elementData[location];
            elementCount--;
            int size = elementCount - location;
            if (size > 0) {
                System.arraycopy(elementData, location + 1, elementData,
                        location, size);
            }
            elementData[elementCount] = null;
            modCount++;
            return result;
        }
        throw new ArrayIndexOutOfBoundsException(location);
    
public booleanremove(java.lang.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.

param
object the object to remove from this vector.
return
{@code true} if the specified object was found, {@code false} otherwise.
see
#removeAllElements
see
#removeElementAt
see
#size
since
Android 1.0

        return removeElement(object);
    
public synchronized booleanremoveAll(java.util.Collection collection)
Removes all occurrences in this vector of each object in the specified Collection.

param
collection the collection of objects to remove.
return
{@code true} if this vector is modified, {@code false} otherwise.
see
#remove(Object)
see
#contains(Object)
since
Android 1.0

        return super.removeAll(collection);
    
public synchronized voidremoveAllElements()
Removes all elements from this vector, leaving the size zero and the capacity unchanged.

see
#isEmpty
see
#size
since
Android 1.0

        Arrays.fill(elementData, 0, elementCount, null);
        modCount++;
        elementCount = 0;
    
public synchronized booleanremoveElement(java.lang.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.

param
object the object to remove from this vector.
return
{@code true} if the specified object was found, {@code false} otherwise.
see
#removeAllElements
see
#removeElementAt
see
#size
since
Android 1.0

        int index;
        if ((index = indexOf(object, 0)) == -1) {
            return false;
        }
        removeElementAt(index);
        return true;
    
public synchronized voidremoveElementAt(int location)
Removes the element found at index position {@code location} from this {@code Vector}. All elements with an index bigger than {@code location} have their index decreased by 1.

param
location the index of the element to remove.
exception
ArrayIndexOutOfBoundsException when {@code location < 0 || location >= size()}.
see
#removeElement
see
#removeAllElements
see
#size
since
Android 1.0

        if (0 <= location && location < elementCount) {
            elementCount--;
            int size = elementCount - location;
            if (size > 0) {
                System.arraycopy(elementData, location + 1, elementData,
                        location, size);
            }
            elementData[elementCount] = null;
            modCount++;
        } else {
            throw new ArrayIndexOutOfBoundsException(location);
        }
    
protected voidremoveRange(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 {@code end} have their index decreased by {@code end - start}.

param
start the index at which to start removing.
param
end the index one past the end of the range to remove.
exception
IndexOutOfBoundsException when {@code start < 0, start > end} or {@code end > size()}.
since
Android 1.0

        if (start >= 0 && start <= end && end <= size()) {
            if (start == end) {
                return;
            }
            if (end != elementCount) {
                System.arraycopy(elementData, end, elementData, start,
                        elementCount - end);
                int newCount = elementCount - (end - start);
                Arrays.fill(elementData, newCount, elementCount, null);
                elementCount = newCount;
            } else {
                Arrays.fill(elementData, start, elementCount, null);
                elementCount = start;
            }
            modCount++;
        } else {
            throw new IndexOutOfBoundsException();
        }
    
public synchronized booleanretainAll(java.util.Collection collection)
Removes all objects from this vector that are not contained in the specified collection.

param
collection the collection of objects to retain.
return
{@code true} if this vector is modified, {@code false} otherwise.
see
#remove(Object)
since
Android 1.0

        return super.retainAll(collection);
    
public synchronized Eset(int location, E object)
Replaces the element at the specified location in this vector with the specified object.

param
location the index at which to put the specified object.
param
object the object to add to this vector.
return
the previous element at the location.
exception
ArrayIndexOutOfBoundsException when {@code location < 0 || location >= size()}.
see
#size
since
Android 1.0

        if (location < elementCount) {
            E result = (E)elementData[location];
            elementData[location] = object;
            return result;
        }
        throw new ArrayIndexOutOfBoundsException(location);
    
public synchronized voidsetElementAt(E object, int location)
Replaces the element at the specified location in this vector with the specified object.

param
object the object to add to this vector.
param
location the index at which to put the specified object.
exception
ArrayIndexOutOfBoundsException when {@code location < 0 || location >= size()}.
see
#size
since
Android 1.0

        if (location < elementCount) {
            elementData[location] = object;
        } else {
            throw new ArrayIndexOutOfBoundsException(location);
        }
    
public synchronized voidsetSize(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.

param
length the new size of this vector.
see
#size
since
Android 1.0

        if (length == elementCount) {
            return;
        }
        ensureCapacity(length);
        if (elementCount > length) {
            Arrays.fill(elementData, length, elementCount, null);
        }
        elementCount = length;
        modCount++;
    
public synchronized intsize()
Returns the number of elements in this vector.

return
the number of elements in this vector.
see
#elementCount
see
#lastElement
since
Android 1.0

        return elementCount;
    
public synchronized java.util.ListsubList(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.

param
start the index at which to start the sublist.
param
end the index one past the end of the sublist.
return
a List of a portion of this vector.
exception
IndexOutOfBoundsException when {@code start < 0} or {@code end > size()}.
exception
IllegalArgumentException when {@code start > end}.
since
Android 1.0

        return new Collections.SynchronizedRandomAccessList<E>(
                super.subList(start, end), this);
    
public synchronized java.lang.Object[]toArray()
Returns a new array containing all elements contained in this vector.

return
an array of the elements from this vector.
since
Android 1.0

        Object[] result = new Object[elementCount];
        System.arraycopy(elementData, 0, result, 0, elementCount);
        return result;
    
public synchronized 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.

param
contents the array to fill.
return
an array of the elements from this vector.
exception
ArrayStoreException when the type of an element in this vector cannot be stored in the type of the specified array.
since
Android 1.0

        if (elementCount > contents.length) {
            Class<?> ct = contents.getClass().getComponentType();
            contents = (T[]) Array.newInstance(ct, elementCount);
        }
        System.arraycopy(elementData, 0, contents, 0, elementCount);
        if (elementCount < contents.length) {
            contents[elementCount] = null;
        }
        return contents;
    
public synchronized java.lang.StringtoString()
Returns the string representation of this vector.

return
the string representation of this vector.
see
#elements
since
Android 1.0

        if (elementCount == 0) {
            return "[]";
        }
        int length = elementCount - 1;
        StringBuffer buffer = new StringBuffer(size() * 16);
        buffer.append('[");
        for (int i = 0; i < length; i++) {
            if (elementData[i] == this) {
                buffer.append("(this Collection)");
            } else {
                buffer.append(elementData[i]);
            }
            buffer.append(", ");
        }
        if (elementData[length] == this) {
            buffer.append("(this Collection)");
        } else {
            buffer.append(elementData[length]);
        }
        buffer.append(']");
        return buffer.toString();
    
public synchronized voidtrimToSize()
Sets the capacity of this vector to be the same as the size.

see
#capacity
see
#ensureCapacity
see
#size
since
Android 1.0

        if (elementData.length != elementCount) {
            grow(elementCount);
        }
    
private synchronized voidwriteObject(java.io.ObjectOutputStream stream)

        stream.defaultWriteObject();