jdk/src/share/classes/java/util/ArrayList.java
author darcy
Fri, 19 Aug 2011 17:42:24 -0700
changeset 10350 6d009f117062
parent 9503 588cf31d584a
child 10419 12c063b39232
permissions -rw-r--r--
4748706: typos in java.util.Observable documentation Reviewed-by: iris
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7518
diff changeset
     2
 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5466
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5466
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5466
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5466
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5466
diff changeset
    23
 * questions.
2
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
 * Resizable-array implementation of the <tt>List</tt> interface.  Implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * all optional list operations, and permits all elements, including
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * <tt>null</tt>.  In addition to implementing the <tt>List</tt> interface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * this class provides methods to manipulate the size of the array that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * used internally to store the list.  (This class is roughly equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <tt>Vector</tt>, except that it is unsynchronized.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>The <tt>size</tt>, <tt>isEmpty</tt>, <tt>get</tt>, <tt>set</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <tt>iterator</tt>, and <tt>listIterator</tt> operations run in constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * time.  The <tt>add</tt> operation runs in <i>amortized constant time</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * that is, adding n elements requires O(n) time.  All of the other operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * run in linear time (roughly speaking).  The constant factor is low compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * to that for the <tt>LinkedList</tt> implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>Each <tt>ArrayList</tt> instance has a <i>capacity</i>.  The capacity is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * the size of the array used to store the elements in the list.  It is always
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * at least as large as the list size.  As elements are added to an ArrayList,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * its capacity grows automatically.  The details of the growth policy are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * specified beyond the fact that adding an element has constant amortized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * time cost.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>An application can increase the capacity of an <tt>ArrayList</tt> instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * before adding a large number of elements using the <tt>ensureCapacity</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * operation.  This may reduce the amount of incremental reallocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p><strong>Note that this implementation is not synchronized.</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * If multiple threads access an <tt>ArrayList</tt> instance concurrently,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * and at least one of the threads modifies the list structurally, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <i>must</i> be synchronized externally.  (A structural modification is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * any operation that adds or deletes one or more elements, or explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * resizes the backing array; merely setting the value of an element is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * a structural modification.)  This is typically accomplished by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * synchronizing on some object that naturally encapsulates the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * If no such object exists, the list should be "wrapped" using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * {@link Collections#synchronizedList Collections.synchronizedList}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * method.  This is best done at creation time, to prevent accidental
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * unsynchronized access to the list:<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *   List list = Collections.synchronizedList(new ArrayList(...));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <p><a name="fail-fast"/>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * The iterators returned by this class's {@link #iterator() iterator} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * if the list is structurally modified at any time after the iterator is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * created, in any way except through the iterator's own
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * {@link ListIterator#remove() remove} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * {@link ListIterator#add(Object) add} methods, the iterator will throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * {@link ConcurrentModificationException}.  Thus, in the face of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * concurrent modification, the iterator fails quickly and cleanly, rather
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * than risking arbitrary, non-deterministic behavior at an undetermined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * time in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * as it is, generally speaking, impossible to make any hard guarantees in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * throw {@code ConcurrentModificationException} on a best-effort basis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * Therefore, it would be wrong to write a program that depended on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * exception for its correctness:  <i>the fail-fast behavior of iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * should be used only to detect bugs.</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * @author  Neal Gafter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * @see     Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * @see     List
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * @see     LinkedList
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * @see     Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
public class ArrayList<E> extends AbstractList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private static final long serialVersionUID = 8683452581122892189L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * The array buffer into which the elements of the ArrayList are stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * The capacity of the ArrayList is the length of this array buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private transient Object[] elementData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * The size of the ArrayList (the number of elements it contains).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Constructs an empty list with the specified initial capacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 7020
diff changeset
   123
     * @param  initialCapacity  the initial capacity of the list
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 7020
diff changeset
   124
     * @throws IllegalArgumentException if the specified initial capacity
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 7020
diff changeset
   125
     *         is negative
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public ArrayList(int initialCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (initialCapacity < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            throw new IllegalArgumentException("Illegal Capacity: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                               initialCapacity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        this.elementData = new Object[initialCapacity];
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 list with an initial capacity of ten.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public ArrayList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        this(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Constructs a list containing the elements of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * collection, in the order they are returned by the collection's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param c the collection whose elements are to be placed into this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public ArrayList(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        elementData = c.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        size = elementData.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // c.toArray might (incorrectly) not return Object[] (see 6260652)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (elementData.getClass() != Object[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            elementData = Arrays.copyOf(elementData, size, Object[].class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Trims the capacity of this <tt>ArrayList</tt> instance to be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * list's current size.  An application can use this operation to minimize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * the storage of an <tt>ArrayList</tt> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public void trimToSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        int oldCapacity = elementData.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (size < oldCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            elementData = Arrays.copyOf(elementData, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Increases the capacity of this <tt>ArrayList</tt> instance, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * necessary, to ensure that it can hold at least the number of elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * specified by the minimum capacity argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 7020
diff changeset
   176
     * @param   minCapacity   the desired minimum capacity
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public void ensureCapacity(int minCapacity) {
7020
25638687fe82 6992121: StringBuilder.ensureCapacity(int minCap) throws OutOfMemoryError with minCap=Integer.MIN_VALUE
mchung
parents: 5506
diff changeset
   179
        if (minCapacity > 0)
25638687fe82 6992121: StringBuilder.ensureCapacity(int minCap) throws OutOfMemoryError with minCap=Integer.MIN_VALUE
mchung
parents: 5506
diff changeset
   180
            ensureCapacityInternal(minCapacity);
25638687fe82 6992121: StringBuilder.ensureCapacity(int minCap) throws OutOfMemoryError with minCap=Integer.MIN_VALUE
mchung
parents: 5506
diff changeset
   181
    }
25638687fe82 6992121: StringBuilder.ensureCapacity(int minCap) throws OutOfMemoryError with minCap=Integer.MIN_VALUE
mchung
parents: 5506
diff changeset
   182
25638687fe82 6992121: StringBuilder.ensureCapacity(int minCap) throws OutOfMemoryError with minCap=Integer.MIN_VALUE
mchung
parents: 5506
diff changeset
   183
    private void ensureCapacityInternal(int minCapacity) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        modCount++;
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   185
        // overflow-conscious code
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   186
        if (minCapacity - elementData.length > 0)
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   187
            grow(minCapacity);
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   188
    }
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   189
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   190
    /**
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   191
     * The maximum size of array to allocate.
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   192
     * Some VMs reserve some header words in an array.
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   193
     * Attempts to allocate larger arrays may result in
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   194
     * OutOfMemoryError: Requested array size exceeds VM limit
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   195
     */
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   196
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   197
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   198
    /**
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   199
     * Increases the capacity to ensure that it can hold at least the
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   200
     * number of elements specified by the minimum capacity argument.
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   201
     *
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   202
     * @param minCapacity the desired minimum capacity
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   203
     */
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   204
    private void grow(int minCapacity) {
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   205
        // overflow-conscious code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        int oldCapacity = elementData.length;
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   207
        int newCapacity = oldCapacity + (oldCapacity >> 1);
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   208
        if (newCapacity - minCapacity < 0)
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   209
            newCapacity = minCapacity;
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   210
        if (newCapacity - MAX_ARRAY_SIZE > 0)
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   211
            newCapacity = hugeCapacity(minCapacity);
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   212
        // minCapacity is usually close to size, so this is a win:
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   213
        elementData = Arrays.copyOf(elementData, newCapacity);
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   214
    }
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   215
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   216
    private static int hugeCapacity(int minCapacity) {
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   217
        if (minCapacity < 0) // overflow
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   218
            throw new OutOfMemoryError();
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   219
        return (minCapacity > MAX_ARRAY_SIZE) ?
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   220
            Integer.MAX_VALUE :
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2178
diff changeset
   221
            MAX_ARRAY_SIZE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Returns the number of elements in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @return the number of elements in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Returns <tt>true</tt> if this list contains no elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @return <tt>true</tt> if this list contains no elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return size == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Returns <tt>true</tt> if this list contains the specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * More formally, returns <tt>true</tt> if and only if this list contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * at least one element <tt>e</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @param o element whose presence in this list is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @return <tt>true</tt> if this list contains the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return indexOf(o) >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Returns the index of the first occurrence of the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * in this list, or -1 if this list does not contain the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * More formally, returns the lowest index <tt>i</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * or -1 if there is no such index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public int indexOf(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            for (int i = 0; i < size; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                if (elementData[i]==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            for (int i = 0; i < size; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                if (o.equals(elementData[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Returns the index of the last occurrence of the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * in this list, or -1 if this list does not contain the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * More formally, returns the highest index <tt>i</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * or -1 if there is no such index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public int lastIndexOf(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            for (int i = size-1; i >= 0; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                if (elementData[i]==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            for (int i = size-1; i >= 0; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                if (o.equals(elementData[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Returns a shallow copy of this <tt>ArrayList</tt> instance.  (The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * elements themselves are not copied.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @return a clone of this <tt>ArrayList</tt> instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                ArrayList<E> v = (ArrayList<E>) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            v.elementData = Arrays.copyOf(elementData, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            v.modCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            // this shouldn't happen, since we are Cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Returns an array containing all of the elements in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * in proper sequence (from first to last element).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * <p>The returned array will be "safe" in that no references to it are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * maintained by this list.  (In other words, this method must allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * a new array).  The caller is thus free to modify the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * <p>This method acts as bridge between array-based and collection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @return an array containing all of the elements in this list in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *         proper sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return Arrays.copyOf(elementData, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * Returns an array containing all of the elements in this list in proper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * sequence (from first to last element); the runtime type of the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * array is that of the specified array.  If the list fits in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * specified array, it is returned therein.  Otherwise, a new array is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * allocated with the runtime type of the specified array and the size of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * <p>If the list fits in the specified array with room to spare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * (i.e., the array has more elements than the list), the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * the array immediately following the end of the collection is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * <tt>null</tt>.  (This is useful in determining the length of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * list <i>only</i> if the caller knows that the list does not contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * any null elements.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @param a the array into which the elements of the list are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *          same runtime type is allocated for this purpose.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @return an array containing the elements of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @throws ArrayStoreException if the runtime type of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *         is not a supertype of the runtime type of every element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *         this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if (a.length < size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            // Make a new array of a's runtime type, but my contents:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            return (T[]) Arrays.copyOf(elementData, size, a.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        System.arraycopy(elementData, 0, a, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        if (a.length > size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            a[size] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    // Positional Access Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    E elementData(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return (E) elementData[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Returns the element at the specified position in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @param  index index of the element to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @return the element at the specified position in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    public E get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        rangeCheck(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        return elementData(index);
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
     * Replaces the element at the specified position in this list with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * the specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @param index index of the element to replace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @param element element to be stored at the specified position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @return the element previously at the specified position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    public E set(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        rangeCheck(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        E oldValue = elementData(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        elementData[index] = element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * Appends the specified element to the end of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @param e element to be appended to this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @return <tt>true</tt> (as specified by {@link Collection#add})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    public boolean add(E e) {
7020
25638687fe82 6992121: StringBuilder.ensureCapacity(int minCap) throws OutOfMemoryError with minCap=Integer.MIN_VALUE
mchung
parents: 5506
diff changeset
   411
        ensureCapacityInternal(size + 1);  // Increments modCount!!
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        elementData[size++] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * Inserts the specified element at the specified position in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * list. Shifts the element currently at that position (if any) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * any subsequent elements to the right (adds one to their indices).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @param index index at which the specified element is to be inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @param element element to be inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    public void add(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        rangeCheckForAdd(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
7020
25638687fe82 6992121: StringBuilder.ensureCapacity(int minCap) throws OutOfMemoryError with minCap=Integer.MIN_VALUE
mchung
parents: 5506
diff changeset
   428
        ensureCapacityInternal(size + 1);  // Increments modCount!!
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        System.arraycopy(elementData, index, elementData, index + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                         size - index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        elementData[index] = element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * Removes the element at the specified position in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * Shifts any subsequent elements to the left (subtracts one from their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * indices).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @param index the index of the element to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @return the element that was removed from the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    public E remove(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        rangeCheck(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        E oldValue = elementData(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        int numMoved = size - index - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        if (numMoved > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            System.arraycopy(elementData, index+1, elementData, index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                             numMoved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        elementData[--size] = null; // Let gc do its work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * Removes the first occurrence of the specified element from this list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * if it is present.  If the list does not contain the element, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * unchanged.  More formally, removes the element with the lowest index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * <tt>i</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * (if such an element exists).  Returns <tt>true</tt> if this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * contained the specified element (or equivalently, if this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @param o element to be removed from this list, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @return <tt>true</tt> if this list contained the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            for (int index = 0; index < size; index++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                if (elementData[index] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    fastRemove(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            for (int index = 0; index < size; index++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                if (o.equals(elementData[index])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    fastRemove(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * Private remove method that skips bounds checking and does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * return the value removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    private void fastRemove(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        int numMoved = size - index - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        if (numMoved > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            System.arraycopy(elementData, index+1, elementData, index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                             numMoved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        elementData[--size] = null; // Let gc do its work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * Removes all of the elements from this list.  The list will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        // Let gc do its work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        for (int i = 0; i < size; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            elementData[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * Appends all of the elements in the specified collection to the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * this list, in the order that they are returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * specified collection's Iterator.  The behavior of this operation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * undefined if the specified collection is modified while the operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * is in progress.  (This implies that the behavior of this call is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * undefined if the specified collection is this list, and this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * list is nonempty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * @param c collection containing elements to be added to this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @return <tt>true</tt> if this list changed as a result of the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    public boolean addAll(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        Object[] a = c.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        int numNew = a.length;
7020
25638687fe82 6992121: StringBuilder.ensureCapacity(int minCap) throws OutOfMemoryError with minCap=Integer.MIN_VALUE
mchung
parents: 5506
diff changeset
   532
        ensureCapacityInternal(size + numNew);  // Increments modCount
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        System.arraycopy(a, 0, elementData, size, numNew);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        size += numNew;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        return numNew != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * Inserts all of the elements in the specified collection into this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * list, starting at the specified position.  Shifts the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * currently at that position (if any) and any subsequent elements to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * the right (increases their indices).  The new elements will appear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * in the list in the order that they are returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * specified collection's iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * @param index index at which to insert the first element from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *              specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @param c collection containing elements to be added to this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * @return <tt>true</tt> if this list changed as a result of the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    public boolean addAll(int index, Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        rangeCheckForAdd(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        Object[] a = c.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        int numNew = a.length;
7020
25638687fe82 6992121: StringBuilder.ensureCapacity(int minCap) throws OutOfMemoryError with minCap=Integer.MIN_VALUE
mchung
parents: 5506
diff changeset
   558
        ensureCapacityInternal(size + numNew);  // Increments modCount
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        int numMoved = size - index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        if (numMoved > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            System.arraycopy(elementData, index, elementData, index + numNew,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                             numMoved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        System.arraycopy(a, 0, elementData, index, numNew);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        size += numNew;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        return numNew != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * Removes from this list all of the elements whose index is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * Shifts any succeeding elements to the left (reduces their index).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * This call shortens the list by {@code (toIndex - fromIndex)} elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * (If {@code toIndex==fromIndex}, this operation has no effect.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * @throws IndexOutOfBoundsException if {@code fromIndex} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     *         {@code toIndex} is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     *         ({@code fromIndex < 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *          fromIndex >= size() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *          toIndex > size() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *          toIndex < fromIndex})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    protected void removeRange(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        int numMoved = size - toIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        System.arraycopy(elementData, toIndex, elementData, fromIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                         numMoved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        // Let gc do its work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        int newSize = size - (toIndex-fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        while (size != newSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            elementData[--size] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * Checks if the given index is in range.  If not, throws an appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * runtime exception.  This method does *not* check if the index is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * negative: It is always used immediately prior to an array access,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * which throws an ArrayIndexOutOfBoundsException if index is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    private void rangeCheck(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        if (index >= size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * A version of rangeCheck used by add and addAll.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    private void rangeCheckForAdd(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        if (index > size || index < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * Constructs an IndexOutOfBoundsException detail message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * Of the many possible refactorings of the error handling code,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * this "outlining" performs best with both server and client VMs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    private String outOfBoundsMsg(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        return "Index: "+index+", Size: "+size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * Removes from this list all of its elements that are contained in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @param c collection containing elements to be removed from this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * @return {@code true} if this list changed as a result of the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * @throws ClassCastException if the class of an element of this list
9503
588cf31d584a 6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents: 7668
diff changeset
   631
     *         is incompatible with the specified collection
588cf31d584a 6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents: 7668
diff changeset
   632
     * (<a href="Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * @throws NullPointerException if this list contains a null element and the
9503
588cf31d584a 6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents: 7668
diff changeset
   634
     *         specified collection does not permit null elements
588cf31d584a 6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents: 7668
diff changeset
   635
     * (<a href="Collection.html#optional-restrictions">optional</a>),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *         or if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @see Collection#contains(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    public boolean removeAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        return batchRemove(c, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * Retains only the elements in this list that are contained in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * specified collection.  In other words, removes from this list all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * of its elements that are not contained in the specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * @param c collection containing elements to be retained in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * @return {@code true} if this list changed as a result of the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * @throws ClassCastException if the class of an element of this list
9503
588cf31d584a 6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents: 7668
diff changeset
   651
     *         is incompatible with the specified collection
588cf31d584a 6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents: 7668
diff changeset
   652
     * (<a href="Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * @throws NullPointerException if this list contains a null element and the
9503
588cf31d584a 6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents: 7668
diff changeset
   654
     *         specified collection does not permit null elements
588cf31d584a 6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents: 7668
diff changeset
   655
     * (<a href="Collection.html#optional-restrictions">optional</a>),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     *         or if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * @see Collection#contains(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    public boolean retainAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        return batchRemove(c, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    private boolean batchRemove(Collection<?> c, boolean complement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        final Object[] elementData = this.elementData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        int r = 0, w = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        boolean modified = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            for (; r < size; r++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                if (c.contains(elementData[r]) == complement)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                    elementData[w++] = elementData[r];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            // Preserve behavioral compatibility with AbstractCollection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            // even if c.contains() throws.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            if (r != size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                System.arraycopy(elementData, r,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                                 elementData, w,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                                 size - r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                w += size - r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            if (w != size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                for (int i = w; i < size; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                    elementData[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                modCount += size - w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                size = w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                modified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        return modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * Save the state of the <tt>ArrayList</tt> instance to a stream (that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * is, serialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * @serialData The length of the array backing the <tt>ArrayList</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     *             instance is emitted (int), followed by all of its elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     *             (each an <tt>Object</tt>) in the proper order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        throws java.io.IOException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        // Write out element count, and any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        int expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        // Write out array length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        s.writeInt(elementData.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        // Write out all elements in the proper order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        for (int i=0; i<size; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            s.writeObject(elementData[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        if (modCount != expectedModCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * Reconstitute the <tt>ArrayList</tt> instance from a stream (that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        // Read in size, and any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        // Read in array length and allocate array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        int arrayLength = s.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        Object[] a = elementData = new Object[arrayLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        // Read in all elements in the proper order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        for (int i=0; i<size; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            a[i] = s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * Returns a list iterator over the elements in this list (in proper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * sequence), starting at the specified position in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * The specified index indicates the first element that would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * returned by an initial call to {@link ListIterator#next next}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * An initial call to {@link ListIterator#previous previous} would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * return the element with the specified index minus one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    public ListIterator<E> listIterator(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        if (index < 0 || index > size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            throw new IndexOutOfBoundsException("Index: "+index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        return new ListItr(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * Returns a list iterator over the elements in this list (in proper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * sequence).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * @see #listIterator(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    public ListIterator<E> listIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        return new ListItr(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * Returns an iterator over the elements in this list in proper sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * @return an iterator over the elements in this list in proper sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        return new Itr();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * An optimized version of AbstractList.Itr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    private class Itr implements Iterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        int cursor;       // index of next element to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        int lastRet = -1; // index of last element returned; -1 if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        int expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            return cursor != size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            int i = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            if (i >= size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            Object[] elementData = ArrayList.this.elementData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            if (i >= elementData.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            cursor = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            return (E) elementData[lastRet = i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            if (lastRet < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                ArrayList.this.remove(lastRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                cursor = lastRet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            } catch (IndexOutOfBoundsException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                throw new ConcurrentModificationException();
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
        final void checkForComodification() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * An optimized version of AbstractList.ListItr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    private class ListItr extends Itr implements ListIterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        ListItr(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            cursor = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        public boolean hasPrevious() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            return cursor != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        public int nextIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            return cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        public int previousIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            return cursor - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        public E previous() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            int i = cursor - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            if (i < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            Object[] elementData = ArrayList.this.elementData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            if (i >= elementData.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            cursor = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            return (E) elementData[lastRet = i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        public void set(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            if (lastRet < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                ArrayList.this.set(lastRet, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            } catch (IndexOutOfBoundsException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        public void add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                int i = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                ArrayList.this.add(i, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                cursor = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            } catch (IndexOutOfBoundsException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            }
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * Returns a view of the portion of this list between the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.  (If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * {@code fromIndex} and {@code toIndex} are equal, the returned list is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * empty.)  The returned list is backed by this list, so non-structural
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * changes in the returned list are reflected in this list, and vice-versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * The returned list supports all of the optional list operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * <p>This method eliminates the need for explicit range operations (of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * the sort that commonly exist for arrays).  Any operation that expects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * a list can be used as a range operation by passing a subList view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * instead of a whole list.  For example, the following idiom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * removes a range of elements from a list:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     *      list.subList(from, to).clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * Similar idioms may be constructed for {@link #indexOf(Object)} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * {@link #lastIndexOf(Object)}, and all of the algorithms in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * {@link Collections} class can be applied to a subList.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * <p>The semantics of the list returned by this method become undefined if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * the backing list (i.e., this list) is <i>structurally modified</i> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * any way other than via the returned list.  (Structural modifications are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * those that change the size of this list, or otherwise perturb it in such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * a fashion that iterations in progress may yield incorrect results.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    public List<E> subList(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        subListRangeCheck(fromIndex, toIndex, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        return new SubList(this, 0, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    static void subListRangeCheck(int fromIndex, int toIndex, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        if (fromIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        if (toIndex > size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            throw new IndexOutOfBoundsException("toIndex = " + toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        if (fromIndex > toIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            throw new IllegalArgumentException("fromIndex(" + fromIndex +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                                               ") > toIndex(" + toIndex + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    private class SubList extends AbstractList<E> implements RandomAccess {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        private final AbstractList<E> parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        private final int parentOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        private final int offset;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   932
        int size;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        SubList(AbstractList<E> parent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                int offset, int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            this.parent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            this.parentOffset = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            this.offset = offset + fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            this.size = toIndex - fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            this.modCount = ArrayList.this.modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        public E set(int index, E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            rangeCheck(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            E oldValue = ArrayList.this.elementData(offset + index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            ArrayList.this.elementData[offset + index] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        public E get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            rangeCheck(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            return ArrayList.this.elementData(offset + index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            return this.size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        public void add(int index, E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            rangeCheckForAdd(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
            parent.add(parentOffset + index, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            this.modCount = parent.modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            this.size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        public E remove(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            rangeCheck(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            E result = parent.remove(parentOffset + index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            this.modCount = parent.modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            this.size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        protected void removeRange(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            parent.removeRange(parentOffset + fromIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                               parentOffset + toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            this.modCount = parent.modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            this.size -= toIndex - fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        public boolean addAll(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            return addAll(this.size, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        public boolean addAll(int index, Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            rangeCheckForAdd(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            int cSize = c.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            if (cSize==0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            parent.addAll(parentOffset + index, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            this.modCount = parent.modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            this.size += cSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            return listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        public ListIterator<E> listIterator(final int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            rangeCheckForAdd(index);
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1011
            final int offset = this.offset;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            return new ListIterator<E>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                int cursor = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                int lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                int expectedModCount = ArrayList.this.modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                    return cursor != SubList.this.size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                    checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                    int i = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                    if (i >= SubList.this.size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                        throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                    Object[] elementData = ArrayList.this.elementData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                    if (offset + i >= elementData.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                        throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                    cursor = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                    return (E) elementData[offset + (lastRet = i)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                public boolean hasPrevious() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                    return cursor != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                public E previous() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                    checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                    int i = cursor - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                    if (i < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                        throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                    Object[] elementData = ArrayList.this.elementData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                    if (offset + i >= elementData.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                        throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                    cursor = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                    return (E) elementData[offset + (lastRet = i)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                public int nextIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                    return cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                public int previousIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                    return cursor - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                    if (lastRet < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                        throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                    checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                        SubList.this.remove(lastRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                        cursor = lastRet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                        lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                        expectedModCount = ArrayList.this.modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                    } catch (IndexOutOfBoundsException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                        throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                public void set(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                    if (lastRet < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                        throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                    checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                        ArrayList.this.set(offset + lastRet, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                    } catch (IndexOutOfBoundsException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                        throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                public void add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                    checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                        int i = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                        SubList.this.add(i, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                        cursor = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                        lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                        expectedModCount = ArrayList.this.modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                    } catch (IndexOutOfBoundsException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                        throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                final void checkForComodification() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                    if (expectedModCount != ArrayList.this.modCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                        throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        public List<E> subList(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            subListRangeCheck(fromIndex, toIndex, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            return new SubList(this, offset, fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        private void rangeCheck(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            if (index < 0 || index >= this.size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        private void rangeCheckForAdd(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            if (index < 0 || index > this.size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        private String outOfBoundsMsg(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            return "Index: "+index+", Size: "+this.size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        private void checkForComodification() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            if (ArrayList.this.modCount != this.modCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
}