rt/emul/compact/src/main/java/java/util/Vector.java
changeset 772 d382dacfd73f
parent 636 8d0be6a9a809
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/util/Vector.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,1194 @@
     1.4 +/*
     1.5 + * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.util;
    1.30 +
    1.31 +
    1.32 +/**
    1.33 + * The {@code Vector} class implements a growable array of
    1.34 + * objects. Like an array, it contains components that can be
    1.35 + * accessed using an integer index. However, the size of a
    1.36 + * {@code Vector} can grow or shrink as needed to accommodate
    1.37 + * adding and removing items after the {@code Vector} has been created.
    1.38 + *
    1.39 + * <p>Each vector tries to optimize storage management by maintaining a
    1.40 + * {@code capacity} and a {@code capacityIncrement}. The
    1.41 + * {@code capacity} is always at least as large as the vector
    1.42 + * size; it is usually larger because as components are added to the
    1.43 + * vector, the vector's storage increases in chunks the size of
    1.44 + * {@code capacityIncrement}. An application can increase the
    1.45 + * capacity of a vector before inserting a large number of
    1.46 + * components; this reduces the amount of incremental reallocation.
    1.47 + *
    1.48 + * <p><a name="fail-fast"/>
    1.49 + * The iterators returned by this class's {@link #iterator() iterator} and
    1.50 + * {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>:
    1.51 + * if the vector is structurally modified at any time after the iterator is
    1.52 + * created, in any way except through the iterator's own
    1.53 + * {@link ListIterator#remove() remove} or
    1.54 + * {@link ListIterator#add(Object) add} methods, the iterator will throw a
    1.55 + * {@link ConcurrentModificationException}.  Thus, in the face of
    1.56 + * concurrent modification, the iterator fails quickly and cleanly, rather
    1.57 + * than risking arbitrary, non-deterministic behavior at an undetermined
    1.58 + * time in the future.  The {@link Enumeration Enumerations} returned by
    1.59 + * the {@link #elements() elements} method are <em>not</em> fail-fast.
    1.60 + *
    1.61 + * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
    1.62 + * as it is, generally speaking, impossible to make any hard guarantees in the
    1.63 + * presence of unsynchronized concurrent modification.  Fail-fast iterators
    1.64 + * throw {@code ConcurrentModificationException} on a best-effort basis.
    1.65 + * Therefore, it would be wrong to write a program that depended on this
    1.66 + * exception for its correctness:  <i>the fail-fast behavior of iterators
    1.67 + * should be used only to detect bugs.</i>
    1.68 + *
    1.69 + * <p>As of the Java 2 platform v1.2, this class was retrofitted to
    1.70 + * implement the {@link List} interface, making it a member of the
    1.71 + * <a href="{@docRoot}/../technotes/guides/collections/index.html">
    1.72 + * Java Collections Framework</a>.  Unlike the new collection
    1.73 + * implementations, {@code Vector} is synchronized.  If a thread-safe
    1.74 + * implementation is not needed, it is recommended to use {@link
    1.75 + * ArrayList} in place of {@code Vector}.
    1.76 + *
    1.77 + * @author  Lee Boynton
    1.78 + * @author  Jonathan Payne
    1.79 + * @see Collection
    1.80 + * @see LinkedList
    1.81 + * @since   JDK1.0
    1.82 + */
    1.83 +public class Vector<E>
    1.84 +    extends AbstractList<E>
    1.85 +    implements List<E>, RandomAccess, Cloneable, java.io.Serializable
    1.86 +{
    1.87 +    /**
    1.88 +     * The array buffer into which the components of the vector are
    1.89 +     * stored. The capacity of the vector is the length of this array buffer,
    1.90 +     * and is at least large enough to contain all the vector's elements.
    1.91 +     *
    1.92 +     * <p>Any array elements following the last element in the Vector are null.
    1.93 +     *
    1.94 +     * @serial
    1.95 +     */
    1.96 +    protected Object[] elementData;
    1.97 +
    1.98 +    /**
    1.99 +     * The number of valid components in this {@code Vector} object.
   1.100 +     * Components {@code elementData[0]} through
   1.101 +     * {@code elementData[elementCount-1]} are the actual items.
   1.102 +     *
   1.103 +     * @serial
   1.104 +     */
   1.105 +    protected int elementCount;
   1.106 +
   1.107 +    /**
   1.108 +     * The amount by which the capacity of the vector is automatically
   1.109 +     * incremented when its size becomes greater than its capacity.  If
   1.110 +     * the capacity increment is less than or equal to zero, the capacity
   1.111 +     * of the vector is doubled each time it needs to grow.
   1.112 +     *
   1.113 +     * @serial
   1.114 +     */
   1.115 +    protected int capacityIncrement;
   1.116 +
   1.117 +    /** use serialVersionUID from JDK 1.0.2 for interoperability */
   1.118 +    private static final long serialVersionUID = -2767605614048989439L;
   1.119 +
   1.120 +    /**
   1.121 +     * Constructs an empty vector with the specified initial capacity and
   1.122 +     * capacity increment.
   1.123 +     *
   1.124 +     * @param   initialCapacity     the initial capacity of the vector
   1.125 +     * @param   capacityIncrement   the amount by which the capacity is
   1.126 +     *                              increased when the vector overflows
   1.127 +     * @throws IllegalArgumentException if the specified initial capacity
   1.128 +     *         is negative
   1.129 +     */
   1.130 +    public Vector(int initialCapacity, int capacityIncrement) {
   1.131 +        super();
   1.132 +        if (initialCapacity < 0)
   1.133 +            throw new IllegalArgumentException("Illegal Capacity: "+
   1.134 +                                               initialCapacity);
   1.135 +        this.elementData = new Object[initialCapacity];
   1.136 +        this.capacityIncrement = capacityIncrement;
   1.137 +    }
   1.138 +
   1.139 +    /**
   1.140 +     * Constructs an empty vector with the specified initial capacity and
   1.141 +     * with its capacity increment equal to zero.
   1.142 +     *
   1.143 +     * @param   initialCapacity   the initial capacity of the vector
   1.144 +     * @throws IllegalArgumentException if the specified initial capacity
   1.145 +     *         is negative
   1.146 +     */
   1.147 +    public Vector(int initialCapacity) {
   1.148 +        this(initialCapacity, 0);
   1.149 +    }
   1.150 +
   1.151 +    /**
   1.152 +     * Constructs an empty vector so that its internal data array
   1.153 +     * has size {@code 10} and its standard capacity increment is
   1.154 +     * zero.
   1.155 +     */
   1.156 +    public Vector() {
   1.157 +        this(10);
   1.158 +    }
   1.159 +
   1.160 +    /**
   1.161 +     * Constructs a vector containing the elements of the specified
   1.162 +     * collection, in the order they are returned by the collection's
   1.163 +     * iterator.
   1.164 +     *
   1.165 +     * @param c the collection whose elements are to be placed into this
   1.166 +     *       vector
   1.167 +     * @throws NullPointerException if the specified collection is null
   1.168 +     * @since   1.2
   1.169 +     */
   1.170 +    public Vector(Collection<? extends E> c) {
   1.171 +        elementData = c.toArray();
   1.172 +        elementCount = elementData.length;
   1.173 +        // c.toArray might (incorrectly) not return Object[] (see 6260652)
   1.174 +        if (elementData.getClass() != Object[].class)
   1.175 +            elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
   1.176 +    }
   1.177 +
   1.178 +    /**
   1.179 +     * Copies the components of this vector into the specified array.
   1.180 +     * The item at index {@code k} in this vector is copied into
   1.181 +     * component {@code k} of {@code anArray}.
   1.182 +     *
   1.183 +     * @param  anArray the array into which the components get copied
   1.184 +     * @throws NullPointerException if the given array is null
   1.185 +     * @throws IndexOutOfBoundsException if the specified array is not
   1.186 +     *         large enough to hold all the components of this vector
   1.187 +     * @throws ArrayStoreException if a component of this vector is not of
   1.188 +     *         a runtime type that can be stored in the specified array
   1.189 +     * @see #toArray(Object[])
   1.190 +     */
   1.191 +    public synchronized void copyInto(Object[] anArray) {
   1.192 +        System.arraycopy(elementData, 0, anArray, 0, elementCount);
   1.193 +    }
   1.194 +
   1.195 +    /**
   1.196 +     * Trims the capacity of this vector to be the vector's current
   1.197 +     * size. If the capacity of this vector is larger than its current
   1.198 +     * size, then the capacity is changed to equal the size by replacing
   1.199 +     * its internal data array, kept in the field {@code elementData},
   1.200 +     * with a smaller one. An application can use this operation to
   1.201 +     * minimize the storage of a vector.
   1.202 +     */
   1.203 +    public synchronized void trimToSize() {
   1.204 +        modCount++;
   1.205 +        int oldCapacity = elementData.length;
   1.206 +        if (elementCount < oldCapacity) {
   1.207 +            elementData = Arrays.copyOf(elementData, elementCount);
   1.208 +        }
   1.209 +    }
   1.210 +
   1.211 +    /**
   1.212 +     * Increases the capacity of this vector, if necessary, to ensure
   1.213 +     * that it can hold at least the number of components specified by
   1.214 +     * the minimum capacity argument.
   1.215 +     *
   1.216 +     * <p>If the current capacity of this vector is less than
   1.217 +     * {@code minCapacity}, then its capacity is increased by replacing its
   1.218 +     * internal data array, kept in the field {@code elementData}, with a
   1.219 +     * larger one.  The size of the new data array will be the old size plus
   1.220 +     * {@code capacityIncrement}, unless the value of
   1.221 +     * {@code capacityIncrement} is less than or equal to zero, in which case
   1.222 +     * the new capacity will be twice the old capacity; but if this new size
   1.223 +     * is still smaller than {@code minCapacity}, then the new capacity will
   1.224 +     * be {@code minCapacity}.
   1.225 +     *
   1.226 +     * @param minCapacity the desired minimum capacity
   1.227 +     */
   1.228 +    public synchronized void ensureCapacity(int minCapacity) {
   1.229 +        if (minCapacity > 0) {
   1.230 +            modCount++;
   1.231 +            ensureCapacityHelper(minCapacity);
   1.232 +        }
   1.233 +    }
   1.234 +
   1.235 +    /**
   1.236 +     * This implements the unsynchronized semantics of ensureCapacity.
   1.237 +     * Synchronized methods in this class can internally call this
   1.238 +     * method for ensuring capacity without incurring the cost of an
   1.239 +     * extra synchronization.
   1.240 +     *
   1.241 +     * @see #ensureCapacity(int)
   1.242 +     */
   1.243 +    private void ensureCapacityHelper(int minCapacity) {
   1.244 +        // overflow-conscious code
   1.245 +        if (minCapacity - elementData.length > 0)
   1.246 +            grow(minCapacity);
   1.247 +    }
   1.248 +
   1.249 +    /**
   1.250 +     * The maximum size of array to allocate.
   1.251 +     * Some VMs reserve some header words in an array.
   1.252 +     * Attempts to allocate larger arrays may result in
   1.253 +     * OutOfMemoryError: Requested array size exceeds VM limit
   1.254 +     */
   1.255 +    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
   1.256 +
   1.257 +    private void grow(int minCapacity) {
   1.258 +        // overflow-conscious code
   1.259 +        int oldCapacity = elementData.length;
   1.260 +        int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
   1.261 +                                         capacityIncrement : oldCapacity);
   1.262 +        if (newCapacity - minCapacity < 0)
   1.263 +            newCapacity = minCapacity;
   1.264 +        if (newCapacity - MAX_ARRAY_SIZE > 0)
   1.265 +            newCapacity = hugeCapacity(minCapacity);
   1.266 +        elementData = Arrays.copyOf(elementData, newCapacity);
   1.267 +    }
   1.268 +
   1.269 +    private static int hugeCapacity(int minCapacity) {
   1.270 +        if (minCapacity < 0) // overflow
   1.271 +            throw new OutOfMemoryError();
   1.272 +        return (minCapacity > MAX_ARRAY_SIZE) ?
   1.273 +            Integer.MAX_VALUE :
   1.274 +            MAX_ARRAY_SIZE;
   1.275 +    }
   1.276 +
   1.277 +    /**
   1.278 +     * Sets the size of this vector. If the new size is greater than the
   1.279 +     * current size, new {@code null} items are added to the end of
   1.280 +     * the vector. If the new size is less than the current size, all
   1.281 +     * components at index {@code newSize} and greater are discarded.
   1.282 +     *
   1.283 +     * @param  newSize   the new size of this vector
   1.284 +     * @throws ArrayIndexOutOfBoundsException if the new size is negative
   1.285 +     */
   1.286 +    public synchronized void setSize(int newSize) {
   1.287 +        modCount++;
   1.288 +        if (newSize > elementCount) {
   1.289 +            ensureCapacityHelper(newSize);
   1.290 +        } else {
   1.291 +            for (int i = newSize ; i < elementCount ; i++) {
   1.292 +                elementData[i] = null;
   1.293 +            }
   1.294 +        }
   1.295 +        elementCount = newSize;
   1.296 +    }
   1.297 +
   1.298 +    /**
   1.299 +     * Returns the current capacity of this vector.
   1.300 +     *
   1.301 +     * @return  the current capacity (the length of its internal
   1.302 +     *          data array, kept in the field {@code elementData}
   1.303 +     *          of this vector)
   1.304 +     */
   1.305 +    public synchronized int capacity() {
   1.306 +        return elementData.length;
   1.307 +    }
   1.308 +
   1.309 +    /**
   1.310 +     * Returns the number of components in this vector.
   1.311 +     *
   1.312 +     * @return  the number of components in this vector
   1.313 +     */
   1.314 +    public synchronized int size() {
   1.315 +        return elementCount;
   1.316 +    }
   1.317 +
   1.318 +    /**
   1.319 +     * Tests if this vector has no components.
   1.320 +     *
   1.321 +     * @return  {@code true} if and only if this vector has
   1.322 +     *          no components, that is, its size is zero;
   1.323 +     *          {@code false} otherwise.
   1.324 +     */
   1.325 +    public synchronized boolean isEmpty() {
   1.326 +        return elementCount == 0;
   1.327 +    }
   1.328 +
   1.329 +    /**
   1.330 +     * Returns an enumeration of the components of this vector. The
   1.331 +     * returned {@code Enumeration} object will generate all items in
   1.332 +     * this vector. The first item generated is the item at index {@code 0},
   1.333 +     * then the item at index {@code 1}, and so on.
   1.334 +     *
   1.335 +     * @return  an enumeration of the components of this vector
   1.336 +     * @see     Iterator
   1.337 +     */
   1.338 +    public Enumeration<E> elements() {
   1.339 +        return new Enumeration<E>() {
   1.340 +            int count = 0;
   1.341 +
   1.342 +            public boolean hasMoreElements() {
   1.343 +                return count < elementCount;
   1.344 +            }
   1.345 +
   1.346 +            public E nextElement() {
   1.347 +                synchronized (Vector.this) {
   1.348 +                    if (count < elementCount) {
   1.349 +                        return elementData(count++);
   1.350 +                    }
   1.351 +                }
   1.352 +                throw new NoSuchElementException("Vector Enumeration");
   1.353 +            }
   1.354 +        };
   1.355 +    }
   1.356 +
   1.357 +    /**
   1.358 +     * Returns {@code true} if this vector contains the specified element.
   1.359 +     * More formally, returns {@code true} if and only if this vector
   1.360 +     * contains at least one element {@code e} such that
   1.361 +     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
   1.362 +     *
   1.363 +     * @param o element whose presence in this vector is to be tested
   1.364 +     * @return {@code true} if this vector contains the specified element
   1.365 +     */
   1.366 +    public boolean contains(Object o) {
   1.367 +        return indexOf(o, 0) >= 0;
   1.368 +    }
   1.369 +
   1.370 +    /**
   1.371 +     * Returns the index of the first occurrence of the specified element
   1.372 +     * in this vector, or -1 if this vector does not contain the element.
   1.373 +     * More formally, returns the lowest index {@code i} such that
   1.374 +     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
   1.375 +     * or -1 if there is no such index.
   1.376 +     *
   1.377 +     * @param o element to search for
   1.378 +     * @return the index of the first occurrence of the specified element in
   1.379 +     *         this vector, or -1 if this vector does not contain the element
   1.380 +     */
   1.381 +    public int indexOf(Object o) {
   1.382 +        return indexOf(o, 0);
   1.383 +    }
   1.384 +
   1.385 +    /**
   1.386 +     * Returns the index of the first occurrence of the specified element in
   1.387 +     * this vector, searching forwards from {@code index}, or returns -1 if
   1.388 +     * the element is not found.
   1.389 +     * More formally, returns the lowest index {@code i} such that
   1.390 +     * <tt>(i&nbsp;&gt;=&nbsp;index&nbsp;&amp;&amp;&nbsp;(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i))))</tt>,
   1.391 +     * or -1 if there is no such index.
   1.392 +     *
   1.393 +     * @param o element to search for
   1.394 +     * @param index index to start searching from
   1.395 +     * @return the index of the first occurrence of the element in
   1.396 +     *         this vector at position {@code index} or later in the vector;
   1.397 +     *         {@code -1} if the element is not found.
   1.398 +     * @throws IndexOutOfBoundsException if the specified index is negative
   1.399 +     * @see     Object#equals(Object)
   1.400 +     */
   1.401 +    public synchronized int indexOf(Object o, int index) {
   1.402 +        if (o == null) {
   1.403 +            for (int i = index ; i < elementCount ; i++)
   1.404 +                if (elementData[i]==null)
   1.405 +                    return i;
   1.406 +        } else {
   1.407 +            for (int i = index ; i < elementCount ; i++)
   1.408 +                if (o.equals(elementData[i]))
   1.409 +                    return i;
   1.410 +        }
   1.411 +        return -1;
   1.412 +    }
   1.413 +
   1.414 +    /**
   1.415 +     * Returns the index of the last occurrence of the specified element
   1.416 +     * in this vector, or -1 if this vector does not contain the element.
   1.417 +     * More formally, returns the highest index {@code i} such that
   1.418 +     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
   1.419 +     * or -1 if there is no such index.
   1.420 +     *
   1.421 +     * @param o element to search for
   1.422 +     * @return the index of the last occurrence of the specified element in
   1.423 +     *         this vector, or -1 if this vector does not contain the element
   1.424 +     */
   1.425 +    public synchronized int lastIndexOf(Object o) {
   1.426 +        return lastIndexOf(o, elementCount-1);
   1.427 +    }
   1.428 +
   1.429 +    /**
   1.430 +     * Returns the index of the last occurrence of the specified element in
   1.431 +     * this vector, searching backwards from {@code index}, or returns -1 if
   1.432 +     * the element is not found.
   1.433 +     * More formally, returns the highest index {@code i} such that
   1.434 +     * <tt>(i&nbsp;&lt;=&nbsp;index&nbsp;&amp;&amp;&nbsp;(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i))))</tt>,
   1.435 +     * or -1 if there is no such index.
   1.436 +     *
   1.437 +     * @param o element to search for
   1.438 +     * @param index index to start searching backwards from
   1.439 +     * @return the index of the last occurrence of the element at position
   1.440 +     *         less than or equal to {@code index} in this vector;
   1.441 +     *         -1 if the element is not found.
   1.442 +     * @throws IndexOutOfBoundsException if the specified index is greater
   1.443 +     *         than or equal to the current size of this vector
   1.444 +     */
   1.445 +    public synchronized int lastIndexOf(Object o, int index) {
   1.446 +        if (index >= elementCount)
   1.447 +            throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
   1.448 +
   1.449 +        if (o == null) {
   1.450 +            for (int i = index; i >= 0; i--)
   1.451 +                if (elementData[i]==null)
   1.452 +                    return i;
   1.453 +        } else {
   1.454 +            for (int i = index; i >= 0; i--)
   1.455 +                if (o.equals(elementData[i]))
   1.456 +                    return i;
   1.457 +        }
   1.458 +        return -1;
   1.459 +    }
   1.460 +
   1.461 +    /**
   1.462 +     * Returns the component at the specified index.
   1.463 +     *
   1.464 +     * <p>This method is identical in functionality to the {@link #get(int)}
   1.465 +     * method (which is part of the {@link List} interface).
   1.466 +     *
   1.467 +     * @param      index   an index into this vector
   1.468 +     * @return     the component at the specified index
   1.469 +     * @throws ArrayIndexOutOfBoundsException if the index is out of range
   1.470 +     *         ({@code index < 0 || index >= size()})
   1.471 +     */
   1.472 +    public synchronized E elementAt(int index) {
   1.473 +        if (index >= elementCount) {
   1.474 +            throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
   1.475 +        }
   1.476 +
   1.477 +        return elementData(index);
   1.478 +    }
   1.479 +
   1.480 +    /**
   1.481 +     * Returns the first component (the item at index {@code 0}) of
   1.482 +     * this vector.
   1.483 +     *
   1.484 +     * @return     the first component of this vector
   1.485 +     * @throws NoSuchElementException if this vector has no components
   1.486 +     */
   1.487 +    public synchronized E firstElement() {
   1.488 +        if (elementCount == 0) {
   1.489 +            throw new NoSuchElementException();
   1.490 +        }
   1.491 +        return elementData(0);
   1.492 +    }
   1.493 +
   1.494 +    /**
   1.495 +     * Returns the last component of the vector.
   1.496 +     *
   1.497 +     * @return  the last component of the vector, i.e., the component at index
   1.498 +     *          <code>size()&nbsp;-&nbsp;1</code>.
   1.499 +     * @throws NoSuchElementException if this vector is empty
   1.500 +     */
   1.501 +    public synchronized E lastElement() {
   1.502 +        if (elementCount == 0) {
   1.503 +            throw new NoSuchElementException();
   1.504 +        }
   1.505 +        return elementData(elementCount - 1);
   1.506 +    }
   1.507 +
   1.508 +    /**
   1.509 +     * Sets the component at the specified {@code index} of this
   1.510 +     * vector to be the specified object. The previous component at that
   1.511 +     * position is discarded.
   1.512 +     *
   1.513 +     * <p>The index must be a value greater than or equal to {@code 0}
   1.514 +     * and less than the current size of the vector.
   1.515 +     *
   1.516 +     * <p>This method is identical in functionality to the
   1.517 +     * {@link #set(int, Object) set(int, E)}
   1.518 +     * method (which is part of the {@link List} interface). Note that the
   1.519 +     * {@code set} method reverses the order of the parameters, to more closely
   1.520 +     * match array usage.  Note also that the {@code set} method returns the
   1.521 +     * old value that was stored at the specified position.
   1.522 +     *
   1.523 +     * @param      obj     what the component is to be set to
   1.524 +     * @param      index   the specified index
   1.525 +     * @throws ArrayIndexOutOfBoundsException if the index is out of range
   1.526 +     *         ({@code index < 0 || index >= size()})
   1.527 +     */
   1.528 +    public synchronized void setElementAt(E obj, int index) {
   1.529 +        if (index >= elementCount) {
   1.530 +            throw new ArrayIndexOutOfBoundsException(index + " >= " +
   1.531 +                                                     elementCount);
   1.532 +        }
   1.533 +        elementData[index] = obj;
   1.534 +    }
   1.535 +
   1.536 +    /**
   1.537 +     * Deletes the component at the specified index. Each component in
   1.538 +     * this vector with an index greater or equal to the specified
   1.539 +     * {@code index} is shifted downward to have an index one
   1.540 +     * smaller than the value it had previously. The size of this vector
   1.541 +     * is decreased by {@code 1}.
   1.542 +     *
   1.543 +     * <p>The index must be a value greater than or equal to {@code 0}
   1.544 +     * and less than the current size of the vector.
   1.545 +     *
   1.546 +     * <p>This method is identical in functionality to the {@link #remove(int)}
   1.547 +     * method (which is part of the {@link List} interface).  Note that the
   1.548 +     * {@code remove} method returns the old value that was stored at the
   1.549 +     * specified position.
   1.550 +     *
   1.551 +     * @param      index   the index of the object to remove
   1.552 +     * @throws ArrayIndexOutOfBoundsException if the index is out of range
   1.553 +     *         ({@code index < 0 || index >= size()})
   1.554 +     */
   1.555 +    public synchronized void removeElementAt(int index) {
   1.556 +        modCount++;
   1.557 +        if (index >= elementCount) {
   1.558 +            throw new ArrayIndexOutOfBoundsException(index + " >= " +
   1.559 +                                                     elementCount);
   1.560 +        }
   1.561 +        else if (index < 0) {
   1.562 +            throw new ArrayIndexOutOfBoundsException(index);
   1.563 +        }
   1.564 +        int j = elementCount - index - 1;
   1.565 +        if (j > 0) {
   1.566 +            System.arraycopy(elementData, index + 1, elementData, index, j);
   1.567 +        }
   1.568 +        elementCount--;
   1.569 +        elementData[elementCount] = null; /* to let gc do its work */
   1.570 +    }
   1.571 +
   1.572 +    /**
   1.573 +     * Inserts the specified object as a component in this vector at the
   1.574 +     * specified {@code index}. Each component in this vector with
   1.575 +     * an index greater or equal to the specified {@code index} is
   1.576 +     * shifted upward to have an index one greater than the value it had
   1.577 +     * previously.
   1.578 +     *
   1.579 +     * <p>The index must be a value greater than or equal to {@code 0}
   1.580 +     * and less than or equal to the current size of the vector. (If the
   1.581 +     * index is equal to the current size of the vector, the new element
   1.582 +     * is appended to the Vector.)
   1.583 +     *
   1.584 +     * <p>This method is identical in functionality to the
   1.585 +     * {@link #add(int, Object) add(int, E)}
   1.586 +     * method (which is part of the {@link List} interface).  Note that the
   1.587 +     * {@code add} method reverses the order of the parameters, to more closely
   1.588 +     * match array usage.
   1.589 +     *
   1.590 +     * @param      obj     the component to insert
   1.591 +     * @param      index   where to insert the new component
   1.592 +     * @throws ArrayIndexOutOfBoundsException if the index is out of range
   1.593 +     *         ({@code index < 0 || index > size()})
   1.594 +     */
   1.595 +    public synchronized void insertElementAt(E obj, int index) {
   1.596 +        modCount++;
   1.597 +        if (index > elementCount) {
   1.598 +            throw new ArrayIndexOutOfBoundsException(index
   1.599 +                                                     + " > " + elementCount);
   1.600 +        }
   1.601 +        ensureCapacityHelper(elementCount + 1);
   1.602 +        System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
   1.603 +        elementData[index] = obj;
   1.604 +        elementCount++;
   1.605 +    }
   1.606 +
   1.607 +    /**
   1.608 +     * Adds the specified component to the end of this vector,
   1.609 +     * increasing its size by one. The capacity of this vector is
   1.610 +     * increased if its size becomes greater than its capacity.
   1.611 +     *
   1.612 +     * <p>This method is identical in functionality to the
   1.613 +     * {@link #add(Object) add(E)}
   1.614 +     * method (which is part of the {@link List} interface).
   1.615 +     *
   1.616 +     * @param   obj   the component to be added
   1.617 +     */
   1.618 +    public synchronized void addElement(E obj) {
   1.619 +        modCount++;
   1.620 +        ensureCapacityHelper(elementCount + 1);
   1.621 +        elementData[elementCount++] = obj;
   1.622 +    }
   1.623 +
   1.624 +    /**
   1.625 +     * Removes the first (lowest-indexed) occurrence of the argument
   1.626 +     * from this vector. If the object is found in this vector, each
   1.627 +     * component in the vector with an index greater or equal to the
   1.628 +     * object's index is shifted downward to have an index one smaller
   1.629 +     * than the value it had previously.
   1.630 +     *
   1.631 +     * <p>This method is identical in functionality to the
   1.632 +     * {@link #remove(Object)} method (which is part of the
   1.633 +     * {@link List} interface).
   1.634 +     *
   1.635 +     * @param   obj   the component to be removed
   1.636 +     * @return  {@code true} if the argument was a component of this
   1.637 +     *          vector; {@code false} otherwise.
   1.638 +     */
   1.639 +    public synchronized boolean removeElement(Object obj) {
   1.640 +        modCount++;
   1.641 +        int i = indexOf(obj);
   1.642 +        if (i >= 0) {
   1.643 +            removeElementAt(i);
   1.644 +            return true;
   1.645 +        }
   1.646 +        return false;
   1.647 +    }
   1.648 +
   1.649 +    /**
   1.650 +     * Removes all components from this vector and sets its size to zero.
   1.651 +     *
   1.652 +     * <p>This method is identical in functionality to the {@link #clear}
   1.653 +     * method (which is part of the {@link List} interface).
   1.654 +     */
   1.655 +    public synchronized void removeAllElements() {
   1.656 +        modCount++;
   1.657 +        // Let gc do its work
   1.658 +        for (int i = 0; i < elementCount; i++)
   1.659 +            elementData[i] = null;
   1.660 +
   1.661 +        elementCount = 0;
   1.662 +    }
   1.663 +
   1.664 +    /**
   1.665 +     * Returns a clone of this vector. The copy will contain a
   1.666 +     * reference to a clone of the internal data array, not a reference
   1.667 +     * to the original internal data array of this {@code Vector} object.
   1.668 +     *
   1.669 +     * @return  a clone of this vector
   1.670 +     */
   1.671 +    public synchronized Object clone() {
   1.672 +        try {
   1.673 +            @SuppressWarnings("unchecked")
   1.674 +                Vector<E> v = (Vector<E>) super.clone();
   1.675 +            v.elementData = Arrays.copyOf(elementData, elementCount);
   1.676 +            v.modCount = 0;
   1.677 +            return v;
   1.678 +        } catch (CloneNotSupportedException e) {
   1.679 +            // this shouldn't happen, since we are Cloneable
   1.680 +            throw new InternalError();
   1.681 +        }
   1.682 +    }
   1.683 +
   1.684 +    /**
   1.685 +     * Returns an array containing all of the elements in this Vector
   1.686 +     * in the correct order.
   1.687 +     *
   1.688 +     * @since 1.2
   1.689 +     */
   1.690 +    public synchronized Object[] toArray() {
   1.691 +        return Arrays.copyOf(elementData, elementCount);
   1.692 +    }
   1.693 +
   1.694 +    /**
   1.695 +     * Returns an array containing all of the elements in this Vector in the
   1.696 +     * correct order; the runtime type of the returned array is that of the
   1.697 +     * specified array.  If the Vector fits in the specified array, it is
   1.698 +     * returned therein.  Otherwise, a new array is allocated with the runtime
   1.699 +     * type of the specified array and the size of this Vector.
   1.700 +     *
   1.701 +     * <p>If the Vector fits in the specified array with room to spare
   1.702 +     * (i.e., the array has more elements than the Vector),
   1.703 +     * the element in the array immediately following the end of the
   1.704 +     * Vector is set to null.  (This is useful in determining the length
   1.705 +     * of the Vector <em>only</em> if the caller knows that the Vector
   1.706 +     * does not contain any null elements.)
   1.707 +     *
   1.708 +     * @param a the array into which the elements of the Vector are to
   1.709 +     *          be stored, if it is big enough; otherwise, a new array of the
   1.710 +     *          same runtime type is allocated for this purpose.
   1.711 +     * @return an array containing the elements of the Vector
   1.712 +     * @throws ArrayStoreException if the runtime type of a is not a supertype
   1.713 +     * of the runtime type of every element in this Vector
   1.714 +     * @throws NullPointerException if the given array is null
   1.715 +     * @since 1.2
   1.716 +     */
   1.717 +    @SuppressWarnings("unchecked")
   1.718 +    public synchronized <T> T[] toArray(T[] a) {
   1.719 +        if (a.length < elementCount)
   1.720 +            return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass());
   1.721 +
   1.722 +        System.arraycopy(elementData, 0, a, 0, elementCount);
   1.723 +
   1.724 +        if (a.length > elementCount)
   1.725 +            a[elementCount] = null;
   1.726 +
   1.727 +        return a;
   1.728 +    }
   1.729 +
   1.730 +    // Positional Access Operations
   1.731 +
   1.732 +    @SuppressWarnings("unchecked")
   1.733 +    E elementData(int index) {
   1.734 +        return (E) elementData[index];
   1.735 +    }
   1.736 +
   1.737 +    /**
   1.738 +     * Returns the element at the specified position in this Vector.
   1.739 +     *
   1.740 +     * @param index index of the element to return
   1.741 +     * @return object at the specified index
   1.742 +     * @throws ArrayIndexOutOfBoundsException if the index is out of range
   1.743 +     *            ({@code index < 0 || index >= size()})
   1.744 +     * @since 1.2
   1.745 +     */
   1.746 +    public synchronized E get(int index) {
   1.747 +        if (index >= elementCount)
   1.748 +            throw new ArrayIndexOutOfBoundsException(index);
   1.749 +
   1.750 +        return elementData(index);
   1.751 +    }
   1.752 +
   1.753 +    /**
   1.754 +     * Replaces the element at the specified position in this Vector with the
   1.755 +     * specified element.
   1.756 +     *
   1.757 +     * @param index index of the element to replace
   1.758 +     * @param element element to be stored at the specified position
   1.759 +     * @return the element previously at the specified position
   1.760 +     * @throws ArrayIndexOutOfBoundsException if the index is out of range
   1.761 +     *         ({@code index < 0 || index >= size()})
   1.762 +     * @since 1.2
   1.763 +     */
   1.764 +    public synchronized E set(int index, E element) {
   1.765 +        if (index >= elementCount)
   1.766 +            throw new ArrayIndexOutOfBoundsException(index);
   1.767 +
   1.768 +        E oldValue = elementData(index);
   1.769 +        elementData[index] = element;
   1.770 +        return oldValue;
   1.771 +    }
   1.772 +
   1.773 +    /**
   1.774 +     * Appends the specified element to the end of this Vector.
   1.775 +     *
   1.776 +     * @param e element to be appended to this Vector
   1.777 +     * @return {@code true} (as specified by {@link Collection#add})
   1.778 +     * @since 1.2
   1.779 +     */
   1.780 +    public synchronized boolean add(E e) {
   1.781 +        modCount++;
   1.782 +        ensureCapacityHelper(elementCount + 1);
   1.783 +        elementData[elementCount++] = e;
   1.784 +        return true;
   1.785 +    }
   1.786 +
   1.787 +    /**
   1.788 +     * Removes the first occurrence of the specified element in this Vector
   1.789 +     * If the Vector does not contain the element, it is unchanged.  More
   1.790 +     * formally, removes the element with the lowest index i such that
   1.791 +     * {@code (o==null ? get(i)==null : o.equals(get(i)))} (if such
   1.792 +     * an element exists).
   1.793 +     *
   1.794 +     * @param o element to be removed from this Vector, if present
   1.795 +     * @return true if the Vector contained the specified element
   1.796 +     * @since 1.2
   1.797 +     */
   1.798 +    public boolean remove(Object o) {
   1.799 +        return removeElement(o);
   1.800 +    }
   1.801 +
   1.802 +    /**
   1.803 +     * Inserts the specified element at the specified position in this Vector.
   1.804 +     * Shifts the element currently at that position (if any) and any
   1.805 +     * subsequent elements to the right (adds one to their indices).
   1.806 +     *
   1.807 +     * @param index index at which the specified element is to be inserted
   1.808 +     * @param element element to be inserted
   1.809 +     * @throws ArrayIndexOutOfBoundsException if the index is out of range
   1.810 +     *         ({@code index < 0 || index > size()})
   1.811 +     * @since 1.2
   1.812 +     */
   1.813 +    public void add(int index, E element) {
   1.814 +        insertElementAt(element, index);
   1.815 +    }
   1.816 +
   1.817 +    /**
   1.818 +     * Removes the element at the specified position in this Vector.
   1.819 +     * Shifts any subsequent elements to the left (subtracts one from their
   1.820 +     * indices).  Returns the element that was removed from the Vector.
   1.821 +     *
   1.822 +     * @throws ArrayIndexOutOfBoundsException if the index is out of range
   1.823 +     *         ({@code index < 0 || index >= size()})
   1.824 +     * @param index the index of the element to be removed
   1.825 +     * @return element that was removed
   1.826 +     * @since 1.2
   1.827 +     */
   1.828 +    public synchronized E remove(int index) {
   1.829 +        modCount++;
   1.830 +        if (index >= elementCount)
   1.831 +            throw new ArrayIndexOutOfBoundsException(index);
   1.832 +        E oldValue = elementData(index);
   1.833 +
   1.834 +        int numMoved = elementCount - index - 1;
   1.835 +        if (numMoved > 0)
   1.836 +            System.arraycopy(elementData, index+1, elementData, index,
   1.837 +                             numMoved);
   1.838 +        elementData[--elementCount] = null; // Let gc do its work
   1.839 +
   1.840 +        return oldValue;
   1.841 +    }
   1.842 +
   1.843 +    /**
   1.844 +     * Removes all of the elements from this Vector.  The Vector will
   1.845 +     * be empty after this call returns (unless it throws an exception).
   1.846 +     *
   1.847 +     * @since 1.2
   1.848 +     */
   1.849 +    public void clear() {
   1.850 +        removeAllElements();
   1.851 +    }
   1.852 +
   1.853 +    // Bulk Operations
   1.854 +
   1.855 +    /**
   1.856 +     * Returns true if this Vector contains all of the elements in the
   1.857 +     * specified Collection.
   1.858 +     *
   1.859 +     * @param   c a collection whose elements will be tested for containment
   1.860 +     *          in this Vector
   1.861 +     * @return true if this Vector contains all of the elements in the
   1.862 +     *         specified collection
   1.863 +     * @throws NullPointerException if the specified collection is null
   1.864 +     */
   1.865 +    public synchronized boolean containsAll(Collection<?> c) {
   1.866 +        return super.containsAll(c);
   1.867 +    }
   1.868 +
   1.869 +    /**
   1.870 +     * Appends all of the elements in the specified Collection to the end of
   1.871 +     * this Vector, in the order that they are returned by the specified
   1.872 +     * Collection's Iterator.  The behavior of this operation is undefined if
   1.873 +     * the specified Collection is modified while the operation is in progress.
   1.874 +     * (This implies that the behavior of this call is undefined if the
   1.875 +     * specified Collection is this Vector, and this Vector is nonempty.)
   1.876 +     *
   1.877 +     * @param c elements to be inserted into this Vector
   1.878 +     * @return {@code true} if this Vector changed as a result of the call
   1.879 +     * @throws NullPointerException if the specified collection is null
   1.880 +     * @since 1.2
   1.881 +     */
   1.882 +    public synchronized boolean addAll(Collection<? extends E> c) {
   1.883 +        modCount++;
   1.884 +        Object[] a = c.toArray();
   1.885 +        int numNew = a.length;
   1.886 +        ensureCapacityHelper(elementCount + numNew);
   1.887 +        System.arraycopy(a, 0, elementData, elementCount, numNew);
   1.888 +        elementCount += numNew;
   1.889 +        return numNew != 0;
   1.890 +    }
   1.891 +
   1.892 +    /**
   1.893 +     * Removes from this Vector all of its elements that are contained in the
   1.894 +     * specified Collection.
   1.895 +     *
   1.896 +     * @param c a collection of elements to be removed from the Vector
   1.897 +     * @return true if this Vector changed as a result of the call
   1.898 +     * @throws ClassCastException if the types of one or more elements
   1.899 +     *         in this vector are incompatible with the specified
   1.900 +     *         collection
   1.901 +     * (<a href="Collection.html#optional-restrictions">optional</a>)
   1.902 +     * @throws NullPointerException if this vector contains one or more null
   1.903 +     *         elements and the specified collection does not support null
   1.904 +     *         elements
   1.905 +     * (<a href="Collection.html#optional-restrictions">optional</a>),
   1.906 +     *         or if the specified collection is null
   1.907 +     * @since 1.2
   1.908 +     */
   1.909 +    public synchronized boolean removeAll(Collection<?> c) {
   1.910 +        return super.removeAll(c);
   1.911 +    }
   1.912 +
   1.913 +    /**
   1.914 +     * Retains only the elements in this Vector that are contained in the
   1.915 +     * specified Collection.  In other words, removes from this Vector all
   1.916 +     * of its elements that are not contained in the specified Collection.
   1.917 +     *
   1.918 +     * @param c a collection of elements to be retained in this Vector
   1.919 +     *          (all other elements are removed)
   1.920 +     * @return true if this Vector changed as a result of the call
   1.921 +     * @throws ClassCastException if the types of one or more elements
   1.922 +     *         in this vector are incompatible with the specified
   1.923 +     *         collection
   1.924 +     * (<a href="Collection.html#optional-restrictions">optional</a>)
   1.925 +     * @throws NullPointerException if this vector contains one or more null
   1.926 +     *         elements and the specified collection does not support null
   1.927 +     *         elements
   1.928 +     *         (<a href="Collection.html#optional-restrictions">optional</a>),
   1.929 +     *         or if the specified collection is null
   1.930 +     * @since 1.2
   1.931 +     */
   1.932 +    public synchronized boolean retainAll(Collection<?> c) {
   1.933 +        return super.retainAll(c);
   1.934 +    }
   1.935 +
   1.936 +    /**
   1.937 +     * Inserts all of the elements in the specified Collection into this
   1.938 +     * Vector at the specified position.  Shifts the element currently at
   1.939 +     * that position (if any) and any subsequent elements to the right
   1.940 +     * (increases their indices).  The new elements will appear in the Vector
   1.941 +     * in the order that they are returned by the specified Collection's
   1.942 +     * iterator.
   1.943 +     *
   1.944 +     * @param index index at which to insert the first element from the
   1.945 +     *              specified collection
   1.946 +     * @param c elements to be inserted into this Vector
   1.947 +     * @return {@code true} if this Vector changed as a result of the call
   1.948 +     * @throws ArrayIndexOutOfBoundsException if the index is out of range
   1.949 +     *         ({@code index < 0 || index > size()})
   1.950 +     * @throws NullPointerException if the specified collection is null
   1.951 +     * @since 1.2
   1.952 +     */
   1.953 +    public synchronized boolean addAll(int index, Collection<? extends E> c) {
   1.954 +        modCount++;
   1.955 +        if (index < 0 || index > elementCount)
   1.956 +            throw new ArrayIndexOutOfBoundsException(index);
   1.957 +
   1.958 +        Object[] a = c.toArray();
   1.959 +        int numNew = a.length;
   1.960 +        ensureCapacityHelper(elementCount + numNew);
   1.961 +
   1.962 +        int numMoved = elementCount - index;
   1.963 +        if (numMoved > 0)
   1.964 +            System.arraycopy(elementData, index, elementData, index + numNew,
   1.965 +                             numMoved);
   1.966 +
   1.967 +        System.arraycopy(a, 0, elementData, index, numNew);
   1.968 +        elementCount += numNew;
   1.969 +        return numNew != 0;
   1.970 +    }
   1.971 +
   1.972 +    /**
   1.973 +     * Compares the specified Object with this Vector for equality.  Returns
   1.974 +     * true if and only if the specified Object is also a List, both Lists
   1.975 +     * have the same size, and all corresponding pairs of elements in the two
   1.976 +     * Lists are <em>equal</em>.  (Two elements {@code e1} and
   1.977 +     * {@code e2} are <em>equal</em> if {@code (e1==null ? e2==null :
   1.978 +     * e1.equals(e2))}.)  In other words, two Lists are defined to be
   1.979 +     * equal if they contain the same elements in the same order.
   1.980 +     *
   1.981 +     * @param o the Object to be compared for equality with this Vector
   1.982 +     * @return true if the specified Object is equal to this Vector
   1.983 +     */
   1.984 +    public synchronized boolean equals(Object o) {
   1.985 +        return super.equals(o);
   1.986 +    }
   1.987 +
   1.988 +    /**
   1.989 +     * Returns the hash code value for this Vector.
   1.990 +     */
   1.991 +    public synchronized int hashCode() {
   1.992 +        return super.hashCode();
   1.993 +    }
   1.994 +
   1.995 +    /**
   1.996 +     * Returns a string representation of this Vector, containing
   1.997 +     * the String representation of each element.
   1.998 +     */
   1.999 +    public synchronized String toString() {
  1.1000 +        return super.toString();
  1.1001 +    }
  1.1002 +
  1.1003 +    /**
  1.1004 +     * Returns a view of the portion of this List between fromIndex,
  1.1005 +     * inclusive, and toIndex, exclusive.  (If fromIndex and toIndex are
  1.1006 +     * equal, the returned List is empty.)  The returned List is backed by this
  1.1007 +     * List, so changes in the returned List are reflected in this List, and
  1.1008 +     * vice-versa.  The returned List supports all of the optional List
  1.1009 +     * operations supported by this List.
  1.1010 +     *
  1.1011 +     * <p>This method eliminates the need for explicit range operations (of
  1.1012 +     * the sort that commonly exist for arrays).  Any operation that expects
  1.1013 +     * a List can be used as a range operation by operating on a subList view
  1.1014 +     * instead of a whole List.  For example, the following idiom
  1.1015 +     * removes a range of elements from a List:
  1.1016 +     * <pre>
  1.1017 +     *      list.subList(from, to).clear();
  1.1018 +     * </pre>
  1.1019 +     * Similar idioms may be constructed for indexOf and lastIndexOf,
  1.1020 +     * and all of the algorithms in the Collections class can be applied to
  1.1021 +     * a subList.
  1.1022 +     *
  1.1023 +     * <p>The semantics of the List returned by this method become undefined if
  1.1024 +     * the backing list (i.e., this List) is <i>structurally modified</i> in
  1.1025 +     * any way other than via the returned List.  (Structural modifications are
  1.1026 +     * those that change the size of the List, or otherwise perturb it in such
  1.1027 +     * a fashion that iterations in progress may yield incorrect results.)
  1.1028 +     *
  1.1029 +     * @param fromIndex low endpoint (inclusive) of the subList
  1.1030 +     * @param toIndex high endpoint (exclusive) of the subList
  1.1031 +     * @return a view of the specified range within this List
  1.1032 +     * @throws IndexOutOfBoundsException if an endpoint index value is out of range
  1.1033 +     *         {@code (fromIndex < 0 || toIndex > size)}
  1.1034 +     * @throws IllegalArgumentException if the endpoint indices are out of order
  1.1035 +     *         {@code (fromIndex > toIndex)}
  1.1036 +     */
  1.1037 +    public synchronized List<E> subList(int fromIndex, int toIndex) {
  1.1038 +        return Collections.synchronizedList(super.subList(fromIndex, toIndex),
  1.1039 +                                            this);
  1.1040 +    }
  1.1041 +
  1.1042 +    /**
  1.1043 +     * Removes from this list all of the elements whose index is between
  1.1044 +     * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
  1.1045 +     * Shifts any succeeding elements to the left (reduces their index).
  1.1046 +     * This call shortens the list by {@code (toIndex - fromIndex)} elements.
  1.1047 +     * (If {@code toIndex==fromIndex}, this operation has no effect.)
  1.1048 +     */
  1.1049 +    protected synchronized void removeRange(int fromIndex, int toIndex) {
  1.1050 +        modCount++;
  1.1051 +        int numMoved = elementCount - toIndex;
  1.1052 +        System.arraycopy(elementData, toIndex, elementData, fromIndex,
  1.1053 +                         numMoved);
  1.1054 +
  1.1055 +        // Let gc do its work
  1.1056 +        int newElementCount = elementCount - (toIndex-fromIndex);
  1.1057 +        while (elementCount != newElementCount)
  1.1058 +            elementData[--elementCount] = null;
  1.1059 +    }
  1.1060 +
  1.1061 +    /**
  1.1062 +     * Returns a list iterator over the elements in this list (in proper
  1.1063 +     * sequence), starting at the specified position in the list.
  1.1064 +     * The specified index indicates the first element that would be
  1.1065 +     * returned by an initial call to {@link ListIterator#next next}.
  1.1066 +     * An initial call to {@link ListIterator#previous previous} would
  1.1067 +     * return the element with the specified index minus one.
  1.1068 +     *
  1.1069 +     * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
  1.1070 +     *
  1.1071 +     * @throws IndexOutOfBoundsException {@inheritDoc}
  1.1072 +     */
  1.1073 +    public synchronized ListIterator<E> listIterator(int index) {
  1.1074 +        if (index < 0 || index > elementCount)
  1.1075 +            throw new IndexOutOfBoundsException("Index: "+index);
  1.1076 +        return new ListItr(index);
  1.1077 +    }
  1.1078 +
  1.1079 +    /**
  1.1080 +     * Returns a list iterator over the elements in this list (in proper
  1.1081 +     * sequence).
  1.1082 +     *
  1.1083 +     * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
  1.1084 +     *
  1.1085 +     * @see #listIterator(int)
  1.1086 +     */
  1.1087 +    public synchronized ListIterator<E> listIterator() {
  1.1088 +        return new ListItr(0);
  1.1089 +    }
  1.1090 +
  1.1091 +    /**
  1.1092 +     * Returns an iterator over the elements in this list in proper sequence.
  1.1093 +     *
  1.1094 +     * <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
  1.1095 +     *
  1.1096 +     * @return an iterator over the elements in this list in proper sequence
  1.1097 +     */
  1.1098 +    public synchronized Iterator<E> iterator() {
  1.1099 +        return new Itr();
  1.1100 +    }
  1.1101 +
  1.1102 +    /**
  1.1103 +     * An optimized version of AbstractList.Itr
  1.1104 +     */
  1.1105 +    private class Itr implements Iterator<E> {
  1.1106 +        int cursor;       // index of next element to return
  1.1107 +        int lastRet = -1; // index of last element returned; -1 if no such
  1.1108 +        int expectedModCount = modCount;
  1.1109 +
  1.1110 +        public boolean hasNext() {
  1.1111 +            // Racy but within spec, since modifications are checked
  1.1112 +            // within or after synchronization in next/previous
  1.1113 +            return cursor != elementCount;
  1.1114 +        }
  1.1115 +
  1.1116 +        public E next() {
  1.1117 +            synchronized (Vector.this) {
  1.1118 +                checkForComodification();
  1.1119 +                int i = cursor;
  1.1120 +                if (i >= elementCount)
  1.1121 +                    throw new NoSuchElementException();
  1.1122 +                cursor = i + 1;
  1.1123 +                return elementData(lastRet = i);
  1.1124 +            }
  1.1125 +        }
  1.1126 +
  1.1127 +        public void remove() {
  1.1128 +            if (lastRet == -1)
  1.1129 +                throw new IllegalStateException();
  1.1130 +            synchronized (Vector.this) {
  1.1131 +                checkForComodification();
  1.1132 +                Vector.this.remove(lastRet);
  1.1133 +                expectedModCount = modCount;
  1.1134 +            }
  1.1135 +            cursor = lastRet;
  1.1136 +            lastRet = -1;
  1.1137 +        }
  1.1138 +
  1.1139 +        final void checkForComodification() {
  1.1140 +            if (modCount != expectedModCount)
  1.1141 +                throw new ConcurrentModificationException();
  1.1142 +        }
  1.1143 +    }
  1.1144 +
  1.1145 +    /**
  1.1146 +     * An optimized version of AbstractList.ListItr
  1.1147 +     */
  1.1148 +    final class ListItr extends Itr implements ListIterator<E> {
  1.1149 +        ListItr(int index) {
  1.1150 +            super();
  1.1151 +            cursor = index;
  1.1152 +        }
  1.1153 +
  1.1154 +        public boolean hasPrevious() {
  1.1155 +            return cursor != 0;
  1.1156 +        }
  1.1157 +
  1.1158 +        public int nextIndex() {
  1.1159 +            return cursor;
  1.1160 +        }
  1.1161 +
  1.1162 +        public int previousIndex() {
  1.1163 +            return cursor - 1;
  1.1164 +        }
  1.1165 +
  1.1166 +        public E previous() {
  1.1167 +            synchronized (Vector.this) {
  1.1168 +                checkForComodification();
  1.1169 +                int i = cursor - 1;
  1.1170 +                if (i < 0)
  1.1171 +                    throw new NoSuchElementException();
  1.1172 +                cursor = i;
  1.1173 +                return elementData(lastRet = i);
  1.1174 +            }
  1.1175 +        }
  1.1176 +
  1.1177 +        public void set(E e) {
  1.1178 +            if (lastRet == -1)
  1.1179 +                throw new IllegalStateException();
  1.1180 +            synchronized (Vector.this) {
  1.1181 +                checkForComodification();
  1.1182 +                Vector.this.set(lastRet, e);
  1.1183 +            }
  1.1184 +        }
  1.1185 +
  1.1186 +        public void add(E e) {
  1.1187 +            int i = cursor;
  1.1188 +            synchronized (Vector.this) {
  1.1189 +                checkForComodification();
  1.1190 +                Vector.this.add(i, e);
  1.1191 +                expectedModCount = modCount;
  1.1192 +            }
  1.1193 +            cursor = i + 1;
  1.1194 +            lastRet = -1;
  1.1195 +        }
  1.1196 +    }
  1.1197 +}