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