jdk/src/share/classes/java/util/Vector.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 64 3244b8bab101
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1994-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * The {@code Vector} class implements a growable array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * objects. Like an array, it contains components that can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * accessed using an integer index. However, the size of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * {@code Vector} can grow or shrink as needed to accommodate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * adding and removing items after the {@code Vector} has been created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p>Each vector tries to optimize storage management by maintaining a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * {@code capacity} and a {@code capacityIncrement}. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * {@code capacity} is always at least as large as the vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * size; it is usually larger because as components are added to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * vector, the vector's storage increases in chunks the size of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * {@code capacityIncrement}. An application can increase the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * capacity of a vector before inserting a large number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * components; this reduces the amount of incremental reallocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p><a name="fail-fast"/>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * The iterators returned by this class's {@link #iterator() iterator} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * if the vector is structurally modified at any time after the iterator is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * created, in any way except through the iterator's own
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * {@link ListIterator#remove() remove} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * {@link ListIterator#add(Object) add} methods, the iterator will throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * {@link ConcurrentModificationException}.  Thus, in the face of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * concurrent modification, the iterator fails quickly and cleanly, rather
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * than risking arbitrary, non-deterministic behavior at an undetermined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * time in the future.  The {@link Enumeration Enumerations} returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * the {@link #elements() elements} method are <em>not</em> fail-fast.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * as it is, generally speaking, impossible to make any hard guarantees in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * throw {@code ConcurrentModificationException} on a best-effort basis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * Therefore, it would be wrong to write a program that depended on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * exception for its correctness:  <i>the fail-fast behavior of iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * should be used only to detect bugs.</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p>As of the Java 2 platform v1.2, this class was retrofitted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * implement the {@link List} interface, making it a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <a href="{@docRoot}/../technotes/guides/collections/index.html"> Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * Collections Framework</a>.  Unlike the new collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * implementations, {@code Vector} is synchronized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @author  Lee Boynton
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * @author  Jonathan Payne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * @see Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * @see List
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @see ArrayList
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * @see LinkedList
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
public class Vector<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    extends AbstractList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    implements List<E>, RandomAccess, Cloneable, java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * The array buffer into which the components of the vector are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * stored. The capacity of the vector is the length of this array buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * and is at least large enough to contain all the vector's elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * <p>Any array elements following the last element in the Vector are null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    protected Object[] elementData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * The number of valid components in this {@code Vector} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Components {@code elementData[0]} through
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * {@code elementData[elementCount-1]} are the actual items.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    protected int elementCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * The amount by which the capacity of the vector is automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * incremented when its size becomes greater than its capacity.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * the capacity increment is less than or equal to zero, the capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * of the vector is doubled each time it needs to grow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    protected int capacityIncrement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private static final long serialVersionUID = -2767605614048989439L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Constructs an empty vector with the specified initial capacity and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * capacity increment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param   initialCapacity     the initial capacity of the vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param   capacityIncrement   the amount by which the capacity is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *                              increased when the vector overflows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @throws IllegalArgumentException if the specified initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *         is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public Vector(int initialCapacity, int capacityIncrement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (initialCapacity < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throw new IllegalArgumentException("Illegal Capacity: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                               initialCapacity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        this.elementData = new Object[initialCapacity];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        this.capacityIncrement = capacityIncrement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Constructs an empty vector with the specified initial capacity and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * with its capacity increment equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param   initialCapacity   the initial capacity of the vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @throws IllegalArgumentException if the specified initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *         is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public Vector(int initialCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        this(initialCapacity, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Constructs an empty vector so that its internal data array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * has size {@code 10} and its standard capacity increment is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public Vector() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        this(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Constructs a vector containing the elements of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * collection, in the order they are returned by the collection's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param c the collection whose elements are to be placed into this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *       vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public Vector(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        elementData = c.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        elementCount = elementData.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        // c.toArray might (incorrectly) not return Object[] (see 6260652)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (elementData.getClass() != Object[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Copies the components of this vector into the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * The item at index {@code k} in this vector is copied into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * component {@code k} of {@code anArray}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @param  anArray the array into which the components get copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @throws NullPointerException if the given array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @throws IndexOutOfBoundsException if the specified array is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *         large enough to hold all the components of this vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @throws ArrayStoreException if a component of this vector is not of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *         a runtime type that can be stored in the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @see #toArray(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public synchronized void copyInto(Object[] anArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        System.arraycopy(elementData, 0, anArray, 0, elementCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Trims the capacity of this vector to be the vector's current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * size. If the capacity of this vector is larger than its current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * size, then the capacity is changed to equal the size by replacing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * its internal data array, kept in the field {@code elementData},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * with a smaller one. An application can use this operation to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * minimize the storage of a vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public synchronized void trimToSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        int oldCapacity = elementData.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (elementCount < oldCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            elementData = Arrays.copyOf(elementData, elementCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Increases the capacity of this vector, if necessary, to ensure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * that it can hold at least the number of components specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * the minimum capacity argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * <p>If the current capacity of this vector is less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * {@code minCapacity}, then its capacity is increased by replacing its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * internal data array, kept in the field {@code elementData}, with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * larger one.  The size of the new data array will be the old size plus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * {@code capacityIncrement}, unless the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * {@code capacityIncrement} is less than or equal to zero, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * the new capacity will be twice the old capacity; but if this new size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * is still smaller than {@code minCapacity}, then the new capacity will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * be {@code minCapacity}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param minCapacity the desired minimum capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public synchronized void ensureCapacity(int minCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        ensureCapacityHelper(minCapacity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * This implements the unsynchronized semantics of ensureCapacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Synchronized methods in this class can internally call this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * method for ensuring capacity without incurring the cost of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * extra synchronization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @see #ensureCapacity(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    private void ensureCapacityHelper(int minCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        int oldCapacity = elementData.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (minCapacity > oldCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            Object[] oldData = elementData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            int newCapacity = (capacityIncrement > 0) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                (oldCapacity + capacityIncrement) : (oldCapacity * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            if (newCapacity < minCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                newCapacity = minCapacity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            elementData = Arrays.copyOf(elementData, newCapacity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Sets the size of this vector. If the new size is greater than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * current size, new {@code null} items are added to the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * the vector. If the new size is less than the current size, all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * components at index {@code newSize} and greater are discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @param  newSize   the new size of this vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @throws ArrayIndexOutOfBoundsException if the new size is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public synchronized void setSize(int newSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (newSize > elementCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            ensureCapacityHelper(newSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            for (int i = newSize ; i < elementCount ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                elementData[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        elementCount = newSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Returns the current capacity of this vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @return  the current capacity (the length of its internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *          data array, kept in the field {@code elementData}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *          of this vector)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public synchronized int capacity() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        return elementData.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Returns the number of components in this vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @return  the number of components in this vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public synchronized int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return elementCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Tests if this vector has no components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @return  {@code true} if and only if this vector has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *          no components, that is, its size is zero;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *          {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public synchronized boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        return elementCount == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Returns an enumeration of the components of this vector. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * returned {@code Enumeration} object will generate all items in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * this vector. The first item generated is the item at index {@code 0},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * then the item at index {@code 1}, and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @return  an enumeration of the components of this vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @see     Iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public Enumeration<E> elements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        return new Enumeration<E>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                return count < elementCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            public E nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                synchronized (Vector.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    if (count < elementCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                        return elementData(count++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                throw new NoSuchElementException("Vector Enumeration");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Returns {@code true} if this vector contains the specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * More formally, returns {@code true} if and only if this vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * contains at least one element {@code e} such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @param o element whose presence in this vector is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @return {@code true} if this vector contains the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return indexOf(o, 0) >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * Returns the index of the first occurrence of the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * in this vector, or -1 if this vector does not contain the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * More formally, returns the lowest index {@code i} such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * or -1 if there is no such index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @param o element to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @return the index of the first occurrence of the specified element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *         this vector, or -1 if this vector does not contain the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public int indexOf(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return indexOf(o, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Returns the index of the first occurrence of the specified element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * this vector, searching forwards from {@code index}, or returns -1 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * the element is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * More formally, returns the lowest index {@code i} such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * <tt>(i&nbsp;&gt;=&nbsp;index&nbsp;&amp;&amp;&nbsp;(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i))))</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * or -1 if there is no such index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @param o element to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @param index index to start searching from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @return the index of the first occurrence of the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *         this vector at position {@code index} or later in the vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *         {@code -1} if the element is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @throws IndexOutOfBoundsException if the specified index is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @see     Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    public synchronized int indexOf(Object o, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            for (int i = index ; i < elementCount ; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                if (elementData[i]==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            for (int i = index ; i < elementCount ; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                if (o.equals(elementData[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * Returns the index of the last occurrence of the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * in this vector, or -1 if this vector does not contain the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * More formally, returns the highest index {@code i} such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * or -1 if there is no such index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @param o element to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @return the index of the last occurrence of the specified element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *         this vector, or -1 if this vector does not contain the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    public synchronized int lastIndexOf(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        return lastIndexOf(o, elementCount-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * Returns the index of the last occurrence of the specified element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * this vector, searching backwards from {@code index}, or returns -1 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * the element is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * More formally, returns the highest index {@code i} such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * <tt>(i&nbsp;&lt;=&nbsp;index&nbsp;&amp;&amp;&nbsp;(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i))))</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * or -1 if there is no such index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @param o element to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @param index index to start searching backwards from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * @return the index of the last occurrence of the element at position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *         less than or equal to {@code index} in this vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *         -1 if the element is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @throws IndexOutOfBoundsException if the specified index is greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *         than or equal to the current size of this vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    public synchronized int lastIndexOf(Object o, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        if (index >= elementCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            for (int i = index; i >= 0; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                if (elementData[i]==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            for (int i = index; i >= 0; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                if (o.equals(elementData[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * Returns the component at the specified index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * <p>This method is identical in functionality to the {@link #get(int)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * method (which is part of the {@link List} interface).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @param      index   an index into this vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @return     the component at the specified index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *         ({@code index < 0 || index >= size()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    public synchronized E elementAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        if (index >= elementCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        return elementData(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * Returns the first component (the item at index {@code 0}) of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * this vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * @return     the first component of this vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @throws NoSuchElementException if this vector has no components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    public synchronized E firstElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        if (elementCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        return elementData(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * Returns the last component of the vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @return  the last component of the vector, i.e., the component at index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *          <code>size()&nbsp;-&nbsp;1</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * @throws NoSuchElementException if this vector is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    public synchronized E lastElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        if (elementCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        return elementData(elementCount - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * Sets the component at the specified {@code index} of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * vector to be the specified object. The previous component at that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * position is discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * <p>The index must be a value greater than or equal to {@code 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * and less than the current size of the vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * <p>This method is identical in functionality to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * {@link #set(int, Object) set(int, E)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * method (which is part of the {@link List} interface). Note that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * {@code set} method reverses the order of the parameters, to more closely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * match array usage.  Note also that the {@code set} method returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * old value that was stored at the specified position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @param      obj     what the component is to be set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @param      index   the specified index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *         ({@code index < 0 || index >= size()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    public synchronized void setElementAt(E obj, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        if (index >= elementCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            throw new ArrayIndexOutOfBoundsException(index + " >= " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                                                     elementCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        elementData[index] = obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * Deletes the component at the specified index. Each component in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * this vector with an index greater or equal to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * {@code index} is shifted downward to have an index one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * smaller than the value it had previously. The size of this vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * is decreased by {@code 1}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * <p>The index must be a value greater than or equal to {@code 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * and less than the current size of the vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * <p>This method is identical in functionality to the {@link #remove(int)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * method (which is part of the {@link List} interface).  Note that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * {@code remove} method returns the old value that was stored at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * specified position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * @param      index   the index of the object to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *         ({@code index < 0 || index >= size()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    public synchronized void removeElementAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        if (index >= elementCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            throw new ArrayIndexOutOfBoundsException(index + " >= " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                                                     elementCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        else if (index < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            throw new ArrayIndexOutOfBoundsException(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        int j = elementCount - index - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        if (j > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            System.arraycopy(elementData, index + 1, elementData, index, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        elementCount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        elementData[elementCount] = null; /* to let gc do its work */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * Inserts the specified object as a component in this vector at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * specified {@code index}. Each component in this vector with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * an index greater or equal to the specified {@code index} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * shifted upward to have an index one greater than the value it had
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * previously.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * <p>The index must be a value greater than or equal to {@code 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * and less than or equal to the current size of the vector. (If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * index is equal to the current size of the vector, the new element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * is appended to the Vector.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * <p>This method is identical in functionality to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * {@link #add(int, Object) add(int, E)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * method (which is part of the {@link List} interface).  Note that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * {@code add} method reverses the order of the parameters, to more closely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * match array usage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * @param      obj     the component to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @param      index   where to insert the new component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *         ({@code index < 0 || index > size()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    public synchronized void insertElementAt(E obj, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        if (index > elementCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            throw new ArrayIndexOutOfBoundsException(index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                                                     + " > " + elementCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        ensureCapacityHelper(elementCount + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        elementData[index] = obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        elementCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * Adds the specified component to the end of this vector,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * increasing its size by one. The capacity of this vector is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * increased if its size becomes greater than its capacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * <p>This method is identical in functionality to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * {@link #add(Object) add(E)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * method (which is part of the {@link List} interface).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * @param   obj   the component to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    public synchronized void addElement(E obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        ensureCapacityHelper(elementCount + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        elementData[elementCount++] = obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * Removes the first (lowest-indexed) occurrence of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * from this vector. If the object is found in this vector, each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * component in the vector with an index greater or equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * object's index is shifted downward to have an index one smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * than the value it had previously.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * <p>This method is identical in functionality to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * {@link #remove(Object)} method (which is part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * {@link List} interface).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * @param   obj   the component to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * @return  {@code true} if the argument was a component of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     *          vector; {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    public synchronized boolean removeElement(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        int i = indexOf(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        if (i >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            removeElementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * Removes all components from this vector and sets its size to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * <p>This method is identical in functionality to the {@link #clear}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * method (which is part of the {@link List} interface).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    public synchronized void removeAllElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        // Let gc do its work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        for (int i = 0; i < elementCount; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            elementData[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        elementCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * Returns a clone of this vector. The copy will contain a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * reference to a clone of the internal data array, not a reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * to the original internal data array of this {@code Vector} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @return  a clone of this vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    public synchronized Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                Vector<E> v = (Vector<E>) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            v.elementData = Arrays.copyOf(elementData, elementCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            v.modCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            // this shouldn't happen, since we are Cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * Returns an array containing all of the elements in this Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * in the correct order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    public synchronized Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        return Arrays.copyOf(elementData, elementCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * Returns an array containing all of the elements in this Vector in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * correct order; the runtime type of the returned array is that of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * specified array.  If the Vector fits in the specified array, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * returned therein.  Otherwise, a new array is allocated with the runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * type of the specified array and the size of this Vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * <p>If the Vector fits in the specified array with room to spare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * (i.e., the array has more elements than the Vector),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * the element in the array immediately following the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * Vector is set to null.  (This is useful in determining the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * of the Vector <em>only</em> if the caller knows that the Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * does not contain any null elements.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * @param a the array into which the elements of the Vector are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     *          same runtime type is allocated for this purpose.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * @return an array containing the elements of the Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * @throws ArrayStoreException if the runtime type of a is not a supertype
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * of the runtime type of every element in this Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @throws NullPointerException if the given array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    public synchronized <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        if (a.length < elementCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        System.arraycopy(elementData, 0, a, 0, elementCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        if (a.length > elementCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            a[elementCount] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    // Positional Access Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    E elementData(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        return (E) elementData[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * Returns the element at the specified position in this Vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * @param index index of the element to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * @return object at the specified index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     *            ({@code index < 0 || index >= size()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    public synchronized E get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        if (index >= elementCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            throw new ArrayIndexOutOfBoundsException(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        return elementData(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * Replaces the element at the specified position in this Vector with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * @param index index of the element to replace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * @param element element to be stored at the specified position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * @return the element previously at the specified position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     *         ({@code index < 0 || index >= size()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    public synchronized E set(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        if (index >= elementCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            throw new ArrayIndexOutOfBoundsException(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        E oldValue = elementData(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        elementData[index] = element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * Appends the specified element to the end of this Vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * @param e element to be appended to this Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * @return {@code true} (as specified by {@link Collection#add})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    public synchronized boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        ensureCapacityHelper(elementCount + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        elementData[elementCount++] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * Removes the first occurrence of the specified element in this Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * If the Vector does not contain the element, it is unchanged.  More
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * formally, removes the element with the lowest index i such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * {@code (o==null ? get(i)==null : o.equals(get(i)))} (if such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * an element exists).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * @param o element to be removed from this Vector, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * @return true if the Vector contained the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        return removeElement(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * Inserts the specified element at the specified position in this Vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * Shifts the element currently at that position (if any) and any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * subsequent elements to the right (adds one to their indices).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * @param index index at which the specified element is to be inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * @param element element to be inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     *         ({@code index < 0 || index > size()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    public void add(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        insertElementAt(element, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * Removes the element at the specified position in this Vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * Shifts any subsequent elements to the left (subtracts one from their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * indices).  Returns the element that was removed from the Vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     *         ({@code index < 0 || index >= size()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * @param index the index of the element to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * @return element that was removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    public synchronized E remove(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        if (index >= elementCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            throw new ArrayIndexOutOfBoundsException(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        E oldValue = elementData(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        int numMoved = elementCount - index - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        if (numMoved > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            System.arraycopy(elementData, index+1, elementData, index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                             numMoved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        elementData[--elementCount] = null; // Let gc do its work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * Removes all of the elements from this Vector.  The Vector will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * be empty after this call returns (unless it throws an exception).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    // Bulk Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * Returns true if this Vector contains all of the elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * specified Collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * @param   c a collection whose elements will be tested for containment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     *          in this Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * @return true if this Vector contains all of the elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     *         specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    public synchronized boolean containsAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        return super.containsAll(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * Appends all of the elements in the specified Collection to the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * this Vector, in the order that they are returned by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * Collection's Iterator.  The behavior of this operation is undefined if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * the specified Collection is modified while the operation is in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * (This implies that the behavior of this call is undefined if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * specified Collection is this Vector, and this Vector is nonempty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * @param c elements to be inserted into this Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * @return {@code true} if this Vector changed as a result of the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    public synchronized boolean addAll(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        Object[] a = c.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        int numNew = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        ensureCapacityHelper(elementCount + numNew);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        System.arraycopy(a, 0, elementData, elementCount, numNew);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        elementCount += numNew;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        return numNew != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * Removes from this Vector all of its elements that are contained in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * specified Collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * @param c a collection of elements to be removed from the Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * @return true if this Vector changed as a result of the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * @throws ClassCastException if the types of one or more elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     *         in this vector are incompatible with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     *         collection (optional)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * @throws NullPointerException if this vector contains one or more null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     *         elements and the specified collection does not support null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     *         elements (optional), or if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    public synchronized boolean removeAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        return super.removeAll(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * Retains only the elements in this Vector that are contained in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * specified Collection.  In other words, removes from this Vector all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * of its elements that are not contained in the specified Collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * @param c a collection of elements to be retained in this Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     *          (all other elements are removed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * @return true if this Vector changed as a result of the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * @throws ClassCastException if the types of one or more elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     *         in this vector are incompatible with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     *         collection (optional)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * @throws NullPointerException if this vector contains one or more null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     *         elements and the specified collection does not support null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     *         elements (optional), or if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    public synchronized boolean retainAll(Collection<?> c)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        return super.retainAll(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * Inserts all of the elements in the specified Collection into this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * Vector at the specified position.  Shifts the element currently at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * that position (if any) and any subsequent elements to the right
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * (increases their indices).  The new elements will appear in the Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * in the order that they are returned by the specified Collection's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * @param index index at which to insert the first element from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     *              specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * @param c elements to be inserted into this Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * @return {@code true} if this Vector changed as a result of the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     *         ({@code index < 0 || index > size()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    public synchronized boolean addAll(int index, Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        if (index < 0 || index > elementCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            throw new ArrayIndexOutOfBoundsException(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        Object[] a = c.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        int numNew = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        ensureCapacityHelper(elementCount + numNew);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        int numMoved = elementCount - index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        if (numMoved > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            System.arraycopy(elementData, index, elementData, index + numNew,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                             numMoved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        System.arraycopy(a, 0, elementData, index, numNew);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        elementCount += numNew;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        return numNew != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * Compares the specified Object with this Vector for equality.  Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * true if and only if the specified Object is also a List, both Lists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * have the same size, and all corresponding pairs of elements in the two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * Lists are <em>equal</em>.  (Two elements {@code e1} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * {@code e2} are <em>equal</em> if {@code (e1==null ? e2==null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * e1.equals(e2))}.)  In other words, two Lists are defined to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * equal if they contain the same elements in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * @param o the Object to be compared for equality with this Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * @return true if the specified Object is equal to this Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    public synchronized boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        return super.equals(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * Returns the hash code value for this Vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    public synchronized int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        return super.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * Returns a string representation of this Vector, containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * the String representation of each element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    public synchronized String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        return super.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * Returns a view of the portion of this List between fromIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * inclusive, and toIndex, exclusive.  (If fromIndex and toIndex are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * equal, the returned List is empty.)  The returned List is backed by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * List, so changes in the returned List are reflected in this List, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * vice-versa.  The returned List supports all of the optional List
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * operations supported by this List.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * <p>This method eliminates the need for explicit range operations (of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * the sort that commonly exist for arrays).  Any operation that expects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * a List can be used as a range operation by operating on a subList view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * instead of a whole List.  For example, the following idiom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * removes a range of elements from a List:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     *      list.subList(from, to).clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * Similar idioms may be constructed for indexOf and lastIndexOf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * and all of the algorithms in the Collections class can be applied to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * a subList.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * <p>The semantics of the List returned by this method become undefined if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * the backing list (i.e., this List) is <i>structurally modified</i> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * any way other than via the returned List.  (Structural modifications are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * those that change the size of the List, or otherwise perturb it in such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * a fashion that iterations in progress may yield incorrect results.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * @param fromIndex low endpoint (inclusive) of the subList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * @param toIndex high endpoint (exclusive) of the subList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * @return a view of the specified range within this List
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * @throws IndexOutOfBoundsException if an endpoint index value is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     *         {@code (fromIndex < 0 || toIndex > size)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * @throws IllegalArgumentException if the endpoint indices are out of order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     *         {@code (fromIndex > toIndex)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    public synchronized List<E> subList(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        return Collections.synchronizedList(super.subList(fromIndex, toIndex),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                                            this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * Removes from this list all of the elements whose index is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * Shifts any succeeding elements to the left (reduces their index).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * This call shortens the list by {@code (toIndex - fromIndex)} elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * (If {@code toIndex==fromIndex}, this operation has no effect.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    protected synchronized void removeRange(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        int numMoved = elementCount - toIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        System.arraycopy(elementData, toIndex, elementData, fromIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                         numMoved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        // Let gc do its work
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        int newElementCount = elementCount - (toIndex-fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        while (elementCount != newElementCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            elementData[--elementCount] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * Save the state of the {@code Vector} instance to a stream (that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * is, serialize it).  This method is present merely for synchronization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * It just calls the default writeObject method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    private synchronized void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        throws java.io.IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * Returns a list iterator over the elements in this list (in proper
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * sequence), starting at the specified position in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * The specified index indicates the first element that would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * returned by an initial call to {@link ListIterator#next next}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * An initial call to {@link ListIterator#previous previous} would
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * return the element with the specified index minus one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    public synchronized ListIterator<E> listIterator(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        if (index < 0 || index > elementCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            throw new IndexOutOfBoundsException("Index: "+index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        return new ListItr(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * Returns a list iterator over the elements in this list (in proper
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * sequence).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * @see #listIterator(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    public synchronized ListIterator<E> listIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        return new ListItr(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * Returns an iterator over the elements in this list in proper sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * @return an iterator over the elements in this list in proper sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    public synchronized Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        return new Itr();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * An optimized version of AbstractList.Itr
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    private class Itr implements Iterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        int cursor;       // index of next element to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        int lastRet = -1; // index of last element returned; -1 if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        int expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            // Racy but within spec, since modifications are checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            // within or after synchronization in next/previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            return cursor != elementCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            synchronized (Vector.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                int i = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                if (i >= elementCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                cursor = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                return elementData(lastRet = i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            if (lastRet == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            synchronized (Vector.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                Vector.this.remove(lastRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            cursor = lastRet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        final void checkForComodification() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * An optimized version of AbstractList.ListItr
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    final class ListItr extends Itr implements ListIterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        ListItr(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            cursor = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        public boolean hasPrevious() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            return cursor != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        public int nextIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            return cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        public int previousIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            return cursor - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        public E previous() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            synchronized (Vector.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                int i = cursor - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                if (i < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                cursor = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                return elementData(lastRet = i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        public void set(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            if (lastRet == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            synchronized (Vector.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                Vector.this.set(lastRet, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        public void add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            int i = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            synchronized (Vector.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                Vector.this.add(i, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            cursor = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
}