jaroslav@597: /* jaroslav@597: * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved. jaroslav@597: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@597: * jaroslav@597: * This code is free software; you can redistribute it and/or modify it jaroslav@597: * under the terms of the GNU General Public License version 2 only, as jaroslav@597: * published by the Free Software Foundation. Oracle designates this jaroslav@597: * particular file as subject to the "Classpath" exception as provided jaroslav@597: * by Oracle in the LICENSE file that accompanied this code. jaroslav@597: * jaroslav@597: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@597: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@597: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@597: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@597: * accompanied this code). jaroslav@597: * jaroslav@597: * You should have received a copy of the GNU General Public License version jaroslav@597: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@597: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@597: * jaroslav@597: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@597: * or visit www.oracle.com if you need additional information or have any jaroslav@597: * questions. jaroslav@597: */ jaroslav@597: jaroslav@597: package java.util; jaroslav@597: jaroslav@599: import org.apidesign.bck2brwsr.emul.lang.System; jaroslav@599: jaroslav@597: /** jaroslav@597: * The {@code Vector} class implements a growable array of jaroslav@597: * objects. Like an array, it contains components that can be jaroslav@597: * accessed using an integer index. However, the size of a jaroslav@597: * {@code Vector} can grow or shrink as needed to accommodate jaroslav@597: * adding and removing items after the {@code Vector} has been created. jaroslav@597: * jaroslav@597: *
Each vector tries to optimize storage management by maintaining a jaroslav@597: * {@code capacity} and a {@code capacityIncrement}. The jaroslav@597: * {@code capacity} is always at least as large as the vector jaroslav@597: * size; it is usually larger because as components are added to the jaroslav@597: * vector, the vector's storage increases in chunks the size of jaroslav@597: * {@code capacityIncrement}. An application can increase the jaroslav@597: * capacity of a vector before inserting a large number of jaroslav@597: * components; this reduces the amount of incremental reallocation. jaroslav@597: * jaroslav@597: *
jaroslav@597: * The iterators returned by this class's {@link #iterator() iterator} and jaroslav@597: * {@link #listIterator(int) listIterator} methods are fail-fast: jaroslav@597: * if the vector is structurally modified at any time after the iterator is jaroslav@597: * created, in any way except through the iterator's own jaroslav@597: * {@link ListIterator#remove() remove} or jaroslav@597: * {@link ListIterator#add(Object) add} methods, the iterator will throw a jaroslav@597: * {@link ConcurrentModificationException}. Thus, in the face of jaroslav@597: * concurrent modification, the iterator fails quickly and cleanly, rather jaroslav@597: * than risking arbitrary, non-deterministic behavior at an undetermined jaroslav@597: * time in the future. The {@link Enumeration Enumerations} returned by jaroslav@597: * the {@link #elements() elements} method are not fail-fast. jaroslav@597: * jaroslav@597: *
Note that the fail-fast behavior of an iterator cannot be guaranteed jaroslav@597: * as it is, generally speaking, impossible to make any hard guarantees in the jaroslav@597: * presence of unsynchronized concurrent modification. Fail-fast iterators jaroslav@597: * throw {@code ConcurrentModificationException} on a best-effort basis. jaroslav@597: * Therefore, it would be wrong to write a program that depended on this jaroslav@597: * exception for its correctness: the fail-fast behavior of iterators jaroslav@597: * should be used only to detect bugs. jaroslav@597: * jaroslav@597: *
As of the Java 2 platform v1.2, this class was retrofitted to
jaroslav@597: * implement the {@link List} interface, making it a member of the
jaroslav@597: *
jaroslav@597: * Java Collections Framework. Unlike the new collection
jaroslav@597: * implementations, {@code Vector} is synchronized. If a thread-safe
jaroslav@597: * implementation is not needed, it is recommended to use {@link
jaroslav@597: * ArrayList} in place of {@code Vector}.
jaroslav@597: *
jaroslav@597: * @author Lee Boynton
jaroslav@597: * @author Jonathan Payne
jaroslav@597: * @see Collection
jaroslav@597: * @see LinkedList
jaroslav@597: * @since JDK1.0
jaroslav@597: */
jaroslav@597: public class Vector Any array elements following the last element in the Vector are null.
jaroslav@597: *
jaroslav@597: * @serial
jaroslav@597: */
jaroslav@597: protected Object[] elementData;
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * The number of valid components in this {@code Vector} object.
jaroslav@597: * Components {@code elementData[0]} through
jaroslav@597: * {@code elementData[elementCount-1]} are the actual items.
jaroslav@597: *
jaroslav@597: * @serial
jaroslav@597: */
jaroslav@597: protected int elementCount;
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * The amount by which the capacity of the vector is automatically
jaroslav@597: * incremented when its size becomes greater than its capacity. If
jaroslav@597: * the capacity increment is less than or equal to zero, the capacity
jaroslav@597: * of the vector is doubled each time it needs to grow.
jaroslav@597: *
jaroslav@597: * @serial
jaroslav@597: */
jaroslav@597: protected int capacityIncrement;
jaroslav@597:
jaroslav@597: /** use serialVersionUID from JDK 1.0.2 for interoperability */
jaroslav@597: private static final long serialVersionUID = -2767605614048989439L;
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Constructs an empty vector with the specified initial capacity and
jaroslav@597: * capacity increment.
jaroslav@597: *
jaroslav@597: * @param initialCapacity the initial capacity of the vector
jaroslav@597: * @param capacityIncrement the amount by which the capacity is
jaroslav@597: * increased when the vector overflows
jaroslav@597: * @throws IllegalArgumentException if the specified initial capacity
jaroslav@597: * is negative
jaroslav@597: */
jaroslav@597: public Vector(int initialCapacity, int capacityIncrement) {
jaroslav@597: super();
jaroslav@597: if (initialCapacity < 0)
jaroslav@597: throw new IllegalArgumentException("Illegal Capacity: "+
jaroslav@597: initialCapacity);
jaroslav@597: this.elementData = new Object[initialCapacity];
jaroslav@597: this.capacityIncrement = capacityIncrement;
jaroslav@597: }
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Constructs an empty vector with the specified initial capacity and
jaroslav@597: * with its capacity increment equal to zero.
jaroslav@597: *
jaroslav@597: * @param initialCapacity the initial capacity of the vector
jaroslav@597: * @throws IllegalArgumentException if the specified initial capacity
jaroslav@597: * is negative
jaroslav@597: */
jaroslav@597: public Vector(int initialCapacity) {
jaroslav@597: this(initialCapacity, 0);
jaroslav@597: }
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Constructs an empty vector so that its internal data array
jaroslav@597: * has size {@code 10} and its standard capacity increment is
jaroslav@597: * zero.
jaroslav@597: */
jaroslav@597: public Vector() {
jaroslav@597: this(10);
jaroslav@597: }
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Constructs a vector containing the elements of the specified
jaroslav@597: * collection, in the order they are returned by the collection's
jaroslav@597: * iterator.
jaroslav@597: *
jaroslav@597: * @param c the collection whose elements are to be placed into this
jaroslav@597: * vector
jaroslav@597: * @throws NullPointerException if the specified collection is null
jaroslav@597: * @since 1.2
jaroslav@597: */
jaroslav@597: public Vector(Collection extends E> c) {
jaroslav@597: elementData = c.toArray();
jaroslav@597: elementCount = elementData.length;
jaroslav@597: // c.toArray might (incorrectly) not return Object[] (see 6260652)
jaroslav@597: if (elementData.getClass() != Object[].class)
jaroslav@597: elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
jaroslav@597: }
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Copies the components of this vector into the specified array.
jaroslav@597: * The item at index {@code k} in this vector is copied into
jaroslav@597: * component {@code k} of {@code anArray}.
jaroslav@597: *
jaroslav@597: * @param anArray the array into which the components get copied
jaroslav@597: * @throws NullPointerException if the given array is null
jaroslav@597: * @throws IndexOutOfBoundsException if the specified array is not
jaroslav@597: * large enough to hold all the components of this vector
jaroslav@597: * @throws ArrayStoreException if a component of this vector is not of
jaroslav@597: * a runtime type that can be stored in the specified array
jaroslav@597: * @see #toArray(Object[])
jaroslav@597: */
jaroslav@597: public synchronized void copyInto(Object[] anArray) {
jaroslav@597: System.arraycopy(elementData, 0, anArray, 0, elementCount);
jaroslav@597: }
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Trims the capacity of this vector to be the vector's current
jaroslav@597: * size. If the capacity of this vector is larger than its current
jaroslav@597: * size, then the capacity is changed to equal the size by replacing
jaroslav@597: * its internal data array, kept in the field {@code elementData},
jaroslav@597: * with a smaller one. An application can use this operation to
jaroslav@597: * minimize the storage of a vector.
jaroslav@597: */
jaroslav@597: public synchronized void trimToSize() {
jaroslav@597: modCount++;
jaroslav@597: int oldCapacity = elementData.length;
jaroslav@597: if (elementCount < oldCapacity) {
jaroslav@597: elementData = Arrays.copyOf(elementData, elementCount);
jaroslav@597: }
jaroslav@597: }
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Increases the capacity of this vector, if necessary, to ensure
jaroslav@597: * that it can hold at least the number of components specified by
jaroslav@597: * the minimum capacity argument.
jaroslav@597: *
jaroslav@597: * If the current capacity of this vector is less than
jaroslav@597: * {@code minCapacity}, then its capacity is increased by replacing its
jaroslav@597: * internal data array, kept in the field {@code elementData}, with a
jaroslav@597: * larger one. The size of the new data array will be the old size plus
jaroslav@597: * {@code capacityIncrement}, unless the value of
jaroslav@597: * {@code capacityIncrement} is less than or equal to zero, in which case
jaroslav@597: * the new capacity will be twice the old capacity; but if this new size
jaroslav@597: * is still smaller than {@code minCapacity}, then the new capacity will
jaroslav@597: * be {@code minCapacity}.
jaroslav@597: *
jaroslav@597: * @param minCapacity the desired minimum capacity
jaroslav@597: */
jaroslav@597: public synchronized void ensureCapacity(int minCapacity) {
jaroslav@597: if (minCapacity > 0) {
jaroslav@597: modCount++;
jaroslav@597: ensureCapacityHelper(minCapacity);
jaroslav@597: }
jaroslav@597: }
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * This implements the unsynchronized semantics of ensureCapacity.
jaroslav@597: * Synchronized methods in this class can internally call this
jaroslav@597: * method for ensuring capacity without incurring the cost of an
jaroslav@597: * extra synchronization.
jaroslav@597: *
jaroslav@597: * @see #ensureCapacity(int)
jaroslav@597: */
jaroslav@597: private void ensureCapacityHelper(int minCapacity) {
jaroslav@597: // overflow-conscious code
jaroslav@597: if (minCapacity - elementData.length > 0)
jaroslav@597: grow(minCapacity);
jaroslav@597: }
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * The maximum size of array to allocate.
jaroslav@597: * Some VMs reserve some header words in an array.
jaroslav@597: * Attempts to allocate larger arrays may result in
jaroslav@597: * OutOfMemoryError: Requested array size exceeds VM limit
jaroslav@597: */
jaroslav@597: private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
jaroslav@597:
jaroslav@597: private void grow(int minCapacity) {
jaroslav@597: // overflow-conscious code
jaroslav@597: int oldCapacity = elementData.length;
jaroslav@597: int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
jaroslav@597: capacityIncrement : oldCapacity);
jaroslav@597: if (newCapacity - minCapacity < 0)
jaroslav@597: newCapacity = minCapacity;
jaroslav@597: if (newCapacity - MAX_ARRAY_SIZE > 0)
jaroslav@597: newCapacity = hugeCapacity(minCapacity);
jaroslav@597: elementData = Arrays.copyOf(elementData, newCapacity);
jaroslav@597: }
jaroslav@597:
jaroslav@597: private static int hugeCapacity(int minCapacity) {
jaroslav@597: if (minCapacity < 0) // overflow
jaroslav@597: throw new OutOfMemoryError();
jaroslav@597: return (minCapacity > MAX_ARRAY_SIZE) ?
jaroslav@597: Integer.MAX_VALUE :
jaroslav@597: MAX_ARRAY_SIZE;
jaroslav@597: }
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Sets the size of this vector. If the new size is greater than the
jaroslav@597: * current size, new {@code null} items are added to the end of
jaroslav@597: * the vector. If the new size is less than the current size, all
jaroslav@597: * components at index {@code newSize} and greater are discarded.
jaroslav@597: *
jaroslav@597: * @param newSize the new size of this vector
jaroslav@597: * @throws ArrayIndexOutOfBoundsException if the new size is negative
jaroslav@597: */
jaroslav@597: public synchronized void setSize(int newSize) {
jaroslav@597: modCount++;
jaroslav@597: if (newSize > elementCount) {
jaroslav@597: ensureCapacityHelper(newSize);
jaroslav@597: } else {
jaroslav@597: for (int i = newSize ; i < elementCount ; i++) {
jaroslav@597: elementData[i] = null;
jaroslav@597: }
jaroslav@597: }
jaroslav@597: elementCount = newSize;
jaroslav@597: }
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Returns the current capacity of this vector.
jaroslav@597: *
jaroslav@597: * @return the current capacity (the length of its internal
jaroslav@597: * data array, kept in the field {@code elementData}
jaroslav@597: * of this vector)
jaroslav@597: */
jaroslav@597: public synchronized int capacity() {
jaroslav@597: return elementData.length;
jaroslav@597: }
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Returns the number of components in this vector.
jaroslav@597: *
jaroslav@597: * @return the number of components in this vector
jaroslav@597: */
jaroslav@597: public synchronized int size() {
jaroslav@597: return elementCount;
jaroslav@597: }
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Tests if this vector has no components.
jaroslav@597: *
jaroslav@597: * @return {@code true} if and only if this vector has
jaroslav@597: * no components, that is, its size is zero;
jaroslav@597: * {@code false} otherwise.
jaroslav@597: */
jaroslav@597: public synchronized boolean isEmpty() {
jaroslav@597: return elementCount == 0;
jaroslav@597: }
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Returns an enumeration of the components of this vector. The
jaroslav@597: * returned {@code Enumeration} object will generate all items in
jaroslav@597: * this vector. The first item generated is the item at index {@code 0},
jaroslav@597: * then the item at index {@code 1}, and so on.
jaroslav@597: *
jaroslav@597: * @return an enumeration of the components of this vector
jaroslav@597: * @see Iterator
jaroslav@597: */
jaroslav@597: public Enumeration This method is identical in functionality to the {@link #get(int)}
jaroslav@597: * method (which is part of the {@link List} interface).
jaroslav@597: *
jaroslav@597: * @param index an index into this vector
jaroslav@597: * @return the component at the specified index
jaroslav@597: * @throws ArrayIndexOutOfBoundsException if the index is out of range
jaroslav@597: * ({@code index < 0 || index >= size()})
jaroslav@597: */
jaroslav@597: public synchronized E elementAt(int index) {
jaroslav@597: if (index >= elementCount) {
jaroslav@597: throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
jaroslav@597: }
jaroslav@597:
jaroslav@597: return elementData(index);
jaroslav@597: }
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Returns the first component (the item at index {@code 0}) of
jaroslav@597: * this vector.
jaroslav@597: *
jaroslav@597: * @return the first component of this vector
jaroslav@597: * @throws NoSuchElementException if this vector has no components
jaroslav@597: */
jaroslav@597: public synchronized E firstElement() {
jaroslav@597: if (elementCount == 0) {
jaroslav@597: throw new NoSuchElementException();
jaroslav@597: }
jaroslav@597: return elementData(0);
jaroslav@597: }
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Returns the last component of the vector.
jaroslav@597: *
jaroslav@597: * @return the last component of the vector, i.e., the component at index
jaroslav@597: * The index must be a value greater than or equal to {@code 0}
jaroslav@597: * and less than the current size of the vector.
jaroslav@597: *
jaroslav@597: * This method is identical in functionality to the
jaroslav@597: * {@link #set(int, Object) set(int, E)}
jaroslav@597: * method (which is part of the {@link List} interface). Note that the
jaroslav@597: * {@code set} method reverses the order of the parameters, to more closely
jaroslav@597: * match array usage. Note also that the {@code set} method returns the
jaroslav@597: * old value that was stored at the specified position.
jaroslav@597: *
jaroslav@597: * @param obj what the component is to be set to
jaroslav@597: * @param index the specified index
jaroslav@597: * @throws ArrayIndexOutOfBoundsException if the index is out of range
jaroslav@597: * ({@code index < 0 || index >= size()})
jaroslav@597: */
jaroslav@597: public synchronized void setElementAt(E obj, int index) {
jaroslav@597: if (index >= elementCount) {
jaroslav@597: throw new ArrayIndexOutOfBoundsException(index + " >= " +
jaroslav@597: elementCount);
jaroslav@597: }
jaroslav@597: elementData[index] = obj;
jaroslav@597: }
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Deletes the component at the specified index. Each component in
jaroslav@597: * this vector with an index greater or equal to the specified
jaroslav@597: * {@code index} is shifted downward to have an index one
jaroslav@597: * smaller than the value it had previously. The size of this vector
jaroslav@597: * is decreased by {@code 1}.
jaroslav@597: *
jaroslav@597: * The index must be a value greater than or equal to {@code 0}
jaroslav@597: * and less than the current size of the vector.
jaroslav@597: *
jaroslav@597: * This method is identical in functionality to the {@link #remove(int)}
jaroslav@597: * method (which is part of the {@link List} interface). Note that the
jaroslav@597: * {@code remove} method returns the old value that was stored at the
jaroslav@597: * specified position.
jaroslav@597: *
jaroslav@597: * @param index the index of the object to remove
jaroslav@597: * @throws ArrayIndexOutOfBoundsException if the index is out of range
jaroslav@597: * ({@code index < 0 || index >= size()})
jaroslav@597: */
jaroslav@597: public synchronized void removeElementAt(int index) {
jaroslav@597: modCount++;
jaroslav@597: if (index >= elementCount) {
jaroslav@597: throw new ArrayIndexOutOfBoundsException(index + " >= " +
jaroslav@597: elementCount);
jaroslav@597: }
jaroslav@597: else if (index < 0) {
jaroslav@597: throw new ArrayIndexOutOfBoundsException(index);
jaroslav@597: }
jaroslav@597: int j = elementCount - index - 1;
jaroslav@597: if (j > 0) {
jaroslav@597: System.arraycopy(elementData, index + 1, elementData, index, j);
jaroslav@597: }
jaroslav@597: elementCount--;
jaroslav@597: elementData[elementCount] = null; /* to let gc do its work */
jaroslav@597: }
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Inserts the specified object as a component in this vector at the
jaroslav@597: * specified {@code index}. Each component in this vector with
jaroslav@597: * an index greater or equal to the specified {@code index} is
jaroslav@597: * shifted upward to have an index one greater than the value it had
jaroslav@597: * previously.
jaroslav@597: *
jaroslav@597: * The index must be a value greater than or equal to {@code 0}
jaroslav@597: * and less than or equal to the current size of the vector. (If the
jaroslav@597: * index is equal to the current size of the vector, the new element
jaroslav@597: * is appended to the Vector.)
jaroslav@597: *
jaroslav@597: * This method is identical in functionality to the
jaroslav@597: * {@link #add(int, Object) add(int, E)}
jaroslav@597: * method (which is part of the {@link List} interface). Note that the
jaroslav@597: * {@code add} method reverses the order of the parameters, to more closely
jaroslav@597: * match array usage.
jaroslav@597: *
jaroslav@597: * @param obj the component to insert
jaroslav@597: * @param index where to insert the new component
jaroslav@597: * @throws ArrayIndexOutOfBoundsException if the index is out of range
jaroslav@597: * ({@code index < 0 || index > size()})
jaroslav@597: */
jaroslav@597: public synchronized void insertElementAt(E obj, int index) {
jaroslav@597: modCount++;
jaroslav@597: if (index > elementCount) {
jaroslav@597: throw new ArrayIndexOutOfBoundsException(index
jaroslav@597: + " > " + elementCount);
jaroslav@597: }
jaroslav@597: ensureCapacityHelper(elementCount + 1);
jaroslav@597: System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
jaroslav@597: elementData[index] = obj;
jaroslav@597: elementCount++;
jaroslav@597: }
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Adds the specified component to the end of this vector,
jaroslav@597: * increasing its size by one. The capacity of this vector is
jaroslav@597: * increased if its size becomes greater than its capacity.
jaroslav@597: *
jaroslav@597: * This method is identical in functionality to the
jaroslav@597: * {@link #add(Object) add(E)}
jaroslav@597: * method (which is part of the {@link List} interface).
jaroslav@597: *
jaroslav@597: * @param obj the component to be added
jaroslav@597: */
jaroslav@597: public synchronized void addElement(E obj) {
jaroslav@597: modCount++;
jaroslav@597: ensureCapacityHelper(elementCount + 1);
jaroslav@597: elementData[elementCount++] = obj;
jaroslav@597: }
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Removes the first (lowest-indexed) occurrence of the argument
jaroslav@597: * from this vector. If the object is found in this vector, each
jaroslav@597: * component in the vector with an index greater or equal to the
jaroslav@597: * object's index is shifted downward to have an index one smaller
jaroslav@597: * than the value it had previously.
jaroslav@597: *
jaroslav@597: * This method is identical in functionality to the
jaroslav@597: * {@link #remove(Object)} method (which is part of the
jaroslav@597: * {@link List} interface).
jaroslav@597: *
jaroslav@597: * @param obj the component to be removed
jaroslav@597: * @return {@code true} if the argument was a component of this
jaroslav@597: * vector; {@code false} otherwise.
jaroslav@597: */
jaroslav@597: public synchronized boolean removeElement(Object obj) {
jaroslav@597: modCount++;
jaroslav@597: int i = indexOf(obj);
jaroslav@597: if (i >= 0) {
jaroslav@597: removeElementAt(i);
jaroslav@597: return true;
jaroslav@597: }
jaroslav@597: return false;
jaroslav@597: }
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Removes all components from this vector and sets its size to zero.
jaroslav@597: *
jaroslav@597: * This method is identical in functionality to the {@link #clear}
jaroslav@597: * method (which is part of the {@link List} interface).
jaroslav@597: */
jaroslav@597: public synchronized void removeAllElements() {
jaroslav@597: modCount++;
jaroslav@597: // Let gc do its work
jaroslav@597: for (int i = 0; i < elementCount; i++)
jaroslav@597: elementData[i] = null;
jaroslav@597:
jaroslav@597: elementCount = 0;
jaroslav@597: }
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Returns a clone of this vector. The copy will contain a
jaroslav@597: * reference to a clone of the internal data array, not a reference
jaroslav@597: * to the original internal data array of this {@code Vector} object.
jaroslav@597: *
jaroslav@597: * @return a clone of this vector
jaroslav@597: */
jaroslav@597: public synchronized Object clone() {
jaroslav@597: try {
jaroslav@597: @SuppressWarnings("unchecked")
jaroslav@597: Vector If the Vector fits in the specified array with room to spare
jaroslav@597: * (i.e., the array has more elements than the Vector),
jaroslav@597: * the element in the array immediately following the end of the
jaroslav@597: * Vector is set to null. (This is useful in determining the length
jaroslav@597: * of the Vector only if the caller knows that the Vector
jaroslav@597: * does not contain any null elements.)
jaroslav@597: *
jaroslav@597: * @param a the array into which the elements of the Vector are to
jaroslav@597: * be stored, if it is big enough; otherwise, a new array of the
jaroslav@597: * same runtime type is allocated for this purpose.
jaroslav@597: * @return an array containing the elements of the Vector
jaroslav@597: * @throws ArrayStoreException if the runtime type of a is not a supertype
jaroslav@597: * of the runtime type of every element in this Vector
jaroslav@597: * @throws NullPointerException if the given array is null
jaroslav@597: * @since 1.2
jaroslav@597: */
jaroslav@597: @SuppressWarnings("unchecked")
jaroslav@597: public synchronized This method eliminates the need for explicit range operations (of
jaroslav@597: * the sort that commonly exist for arrays). Any operation that expects
jaroslav@597: * a List can be used as a range operation by operating on a subList view
jaroslav@597: * instead of a whole List. For example, the following idiom
jaroslav@597: * removes a range of elements from a List:
jaroslav@597: * The semantics of the List returned by this method become undefined if
jaroslav@597: * the backing list (i.e., this List) is structurally modified in
jaroslav@597: * any way other than via the returned List. (Structural modifications are
jaroslav@597: * those that change the size of the List, or otherwise perturb it in such
jaroslav@597: * a fashion that iterations in progress may yield incorrect results.)
jaroslav@597: *
jaroslav@597: * @param fromIndex low endpoint (inclusive) of the subList
jaroslav@597: * @param toIndex high endpoint (exclusive) of the subList
jaroslav@597: * @return a view of the specified range within this List
jaroslav@597: * @throws IndexOutOfBoundsException if an endpoint index value is out of range
jaroslav@597: * {@code (fromIndex < 0 || toIndex > size)}
jaroslav@597: * @throws IllegalArgumentException if the endpoint indices are out of order
jaroslav@597: * {@code (fromIndex > toIndex)}
jaroslav@597: */
jaroslav@597: public synchronized List The returned list iterator is fail-fast.
jaroslav@597: *
jaroslav@597: * @throws IndexOutOfBoundsException {@inheritDoc}
jaroslav@597: */
jaroslav@597: public synchronized ListIterator The returned list iterator is fail-fast.
jaroslav@597: *
jaroslav@597: * @see #listIterator(int)
jaroslav@597: */
jaroslav@597: public synchronized ListIterator The returned iterator is fail-fast.
jaroslav@597: *
jaroslav@597: * @return an iterator over the elements in this list in proper sequence
jaroslav@597: */
jaroslav@597: public synchronized Iteratorsize() - 1
.
jaroslav@597: * @throws NoSuchElementException if this vector is empty
jaroslav@597: */
jaroslav@597: public synchronized E lastElement() {
jaroslav@597: if (elementCount == 0) {
jaroslav@597: throw new NoSuchElementException();
jaroslav@597: }
jaroslav@597: return elementData(elementCount - 1);
jaroslav@597: }
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Sets the component at the specified {@code index} of this
jaroslav@597: * vector to be the specified object. The previous component at that
jaroslav@597: * position is discarded.
jaroslav@597: *
jaroslav@597: *
jaroslav@597: * list.subList(from, to).clear();
jaroslav@597: *
jaroslav@597: * Similar idioms may be constructed for indexOf and lastIndexOf,
jaroslav@597: * and all of the algorithms in the Collections class can be applied to
jaroslav@597: * a subList.
jaroslav@597: *
jaroslav@597: *