0N/A/*
6061N/A * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.util;
0N/A
0N/A/**
0N/A * Resizable-array implementation of the <tt>List</tt> interface. Implements
0N/A * all optional list operations, and permits all elements, including
0N/A * <tt>null</tt>. In addition to implementing the <tt>List</tt> interface,
0N/A * this class provides methods to manipulate the size of the array that is
0N/A * used internally to store the list. (This class is roughly equivalent to
0N/A * <tt>Vector</tt>, except that it is unsynchronized.)
0N/A *
0N/A * <p>The <tt>size</tt>, <tt>isEmpty</tt>, <tt>get</tt>, <tt>set</tt>,
0N/A * <tt>iterator</tt>, and <tt>listIterator</tt> operations run in constant
0N/A * time. The <tt>add</tt> operation runs in <i>amortized constant time</i>,
0N/A * that is, adding n elements requires O(n) time. All of the other operations
0N/A * run in linear time (roughly speaking). The constant factor is low compared
0N/A * to that for the <tt>LinkedList</tt> implementation.
0N/A *
0N/A * <p>Each <tt>ArrayList</tt> instance has a <i>capacity</i>. The capacity is
0N/A * the size of the array used to store the elements in the list. It is always
0N/A * at least as large as the list size. As elements are added to an ArrayList,
0N/A * its capacity grows automatically. The details of the growth policy are not
0N/A * specified beyond the fact that adding an element has constant amortized
0N/A * time cost.
0N/A *
0N/A * <p>An application can increase the capacity of an <tt>ArrayList</tt> instance
0N/A * before adding a large number of elements using the <tt>ensureCapacity</tt>
0N/A * operation. This may reduce the amount of incremental reallocation.
0N/A *
0N/A * <p><strong>Note that this implementation is not synchronized.</strong>
0N/A * If multiple threads access an <tt>ArrayList</tt> instance concurrently,
0N/A * and at least one of the threads modifies the list structurally, it
0N/A * <i>must</i> be synchronized externally. (A structural modification is
0N/A * any operation that adds or deletes one or more elements, or explicitly
0N/A * resizes the backing array; merely setting the value of an element is not
0N/A * a structural modification.) This is typically accomplished by
0N/A * synchronizing on some object that naturally encapsulates the list.
0N/A *
0N/A * If no such object exists, the list should be "wrapped" using the
0N/A * {@link Collections#synchronizedList Collections.synchronizedList}
0N/A * method. This is best done at creation time, to prevent accidental
0N/A * unsynchronized access to the list:<pre>
0N/A * List list = Collections.synchronizedList(new ArrayList(...));</pre>
0N/A *
0N/A * <p><a name="fail-fast"/>
0N/A * The iterators returned by this class's {@link #iterator() iterator} and
0N/A * {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>:
0N/A * if the list is structurally modified at any time after the iterator is
0N/A * created, in any way except through the iterator's own
0N/A * {@link ListIterator#remove() remove} or
0N/A * {@link ListIterator#add(Object) add} methods, the iterator will throw a
0N/A * {@link ConcurrentModificationException}. Thus, in the face of
0N/A * concurrent modification, the iterator fails quickly and cleanly, rather
0N/A * than risking arbitrary, non-deterministic behavior at an undetermined
0N/A * time in the future.
0N/A *
0N/A * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
0N/A * as it is, generally speaking, impossible to make any hard guarantees in the
0N/A * presence of unsynchronized concurrent modification. Fail-fast iterators
0N/A * throw {@code ConcurrentModificationException} on a best-effort basis.
0N/A * Therefore, it would be wrong to write a program that depended on this
0N/A * exception for its correctness: <i>the fail-fast behavior of iterators
0N/A * should be used only to detect bugs.</i>
0N/A *
0N/A * <p>This class is a member of the
0N/A * <a href="{@docRoot}/../technotes/guides/collections/index.html">
0N/A * Java Collections Framework</a>.
0N/A *
0N/A * @author Josh Bloch
0N/A * @author Neal Gafter
0N/A * @see Collection
0N/A * @see List
0N/A * @see LinkedList
0N/A * @see Vector
0N/A * @since 1.2
0N/A */
0N/A
0N/Apublic class ArrayList<E> extends AbstractList<E>
0N/A implements List<E>, RandomAccess, Cloneable, java.io.Serializable
0N/A{
0N/A private static final long serialVersionUID = 8683452581122892189L;
0N/A
0N/A /**
6061N/A * Default initial capacity.
6061N/A */
6061N/A private static final int DEFAULT_CAPACITY = 10;
6061N/A
6061N/A /**
6061N/A * Shared empty array instance used for empty instances.
6061N/A */
6061N/A private static final Object[] EMPTY_ELEMENTDATA = {};
6061N/A
6061N/A /**
0N/A * The array buffer into which the elements of the ArrayList are stored.
6061N/A * The capacity of the ArrayList is the length of this array buffer. Any
6061N/A * empty ArrayList with elementData == EMPTY_ELEMENTDATA will be expanded to
6061N/A * DEFAULT_CAPACITY when the first element is added.
0N/A */
0N/A private transient Object[] elementData;
0N/A
0N/A /**
0N/A * The size of the ArrayList (the number of elements it contains).
0N/A *
0N/A * @serial
0N/A */
0N/A private int size;
0N/A
0N/A /**
0N/A * Constructs an empty list with the specified initial capacity.
0N/A *
3203N/A * @param initialCapacity the initial capacity of the list
3203N/A * @throws IllegalArgumentException if the specified initial capacity
3203N/A * is negative
0N/A */
0N/A public ArrayList(int initialCapacity) {
0N/A super();
0N/A if (initialCapacity < 0)
0N/A throw new IllegalArgumentException("Illegal Capacity: "+
0N/A initialCapacity);
0N/A this.elementData = new Object[initialCapacity];
0N/A }
0N/A
0N/A /**
0N/A * Constructs an empty list with an initial capacity of ten.
0N/A */
0N/A public ArrayList() {
6061N/A super();
6061N/A this.elementData = EMPTY_ELEMENTDATA;
0N/A }
0N/A
0N/A /**
0N/A * Constructs a list containing the elements of the specified
0N/A * collection, in the order they are returned by the collection's
0N/A * iterator.
0N/A *
0N/A * @param c the collection whose elements are to be placed into this list
0N/A * @throws NullPointerException if the specified collection is null
0N/A */
0N/A public ArrayList(Collection<? extends E> c) {
0N/A elementData = c.toArray();
0N/A size = elementData.length;
0N/A // c.toArray might (incorrectly) not return Object[] (see 6260652)
0N/A if (elementData.getClass() != Object[].class)
0N/A elementData = Arrays.copyOf(elementData, size, Object[].class);
0N/A }
0N/A
0N/A /**
0N/A * Trims the capacity of this <tt>ArrayList</tt> instance to be the
0N/A * list's current size. An application can use this operation to minimize
0N/A * the storage of an <tt>ArrayList</tt> instance.
0N/A */
0N/A public void trimToSize() {
0N/A modCount++;
6061N/A if (size < elementData.length) {
0N/A elementData = Arrays.copyOf(elementData, size);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Increases the capacity of this <tt>ArrayList</tt> instance, if
0N/A * necessary, to ensure that it can hold at least the number of elements
0N/A * specified by the minimum capacity argument.
0N/A *
3203N/A * @param minCapacity the desired minimum capacity
0N/A */
0N/A public void ensureCapacity(int minCapacity) {
6061N/A int minExpand = (elementData != EMPTY_ELEMENTDATA)
6061N/A // any size if real element table
6061N/A ? 0
6061N/A // larger than default for empty table. It's already supposed to be
6061N/A // at default size.
6061N/A : DEFAULT_CAPACITY;
6061N/A
6061N/A if (minCapacity > minExpand) {
6061N/A ensureExplicitCapacity(minCapacity);
6061N/A }
2979N/A }
2979N/A
2979N/A private void ensureCapacityInternal(int minCapacity) {
6061N/A if (elementData == EMPTY_ELEMENTDATA) {
6061N/A minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
6061N/A }
6061N/A
6061N/A ensureExplicitCapacity(minCapacity);
6061N/A }
6061N/A
6061N/A private void ensureExplicitCapacity(int minCapacity) {
0N/A modCount++;
6061N/A
2350N/A // overflow-conscious code
2350N/A if (minCapacity - elementData.length > 0)
2350N/A grow(minCapacity);
2350N/A }
2350N/A
2350N/A /**
2350N/A * The maximum size of array to allocate.
2350N/A * Some VMs reserve some header words in an array.
2350N/A * Attempts to allocate larger arrays may result in
2350N/A * OutOfMemoryError: Requested array size exceeds VM limit
2350N/A */
2350N/A private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
2350N/A
2350N/A /**
2350N/A * Increases the capacity to ensure that it can hold at least the
2350N/A * number of elements specified by the minimum capacity argument.
2350N/A *
2350N/A * @param minCapacity the desired minimum capacity
2350N/A */
2350N/A private void grow(int minCapacity) {
2350N/A // overflow-conscious code
0N/A int oldCapacity = elementData.length;
2350N/A int newCapacity = oldCapacity + (oldCapacity >> 1);
2350N/A if (newCapacity - minCapacity < 0)
2350N/A newCapacity = minCapacity;
2350N/A if (newCapacity - MAX_ARRAY_SIZE > 0)
2350N/A newCapacity = hugeCapacity(minCapacity);
2350N/A // minCapacity is usually close to size, so this is a win:
2350N/A elementData = Arrays.copyOf(elementData, newCapacity);
2350N/A }
2350N/A
2350N/A private static int hugeCapacity(int minCapacity) {
2350N/A if (minCapacity < 0) // overflow
2350N/A throw new OutOfMemoryError();
2350N/A return (minCapacity > MAX_ARRAY_SIZE) ?
2350N/A Integer.MAX_VALUE :
2350N/A MAX_ARRAY_SIZE;
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of elements in this list.
0N/A *
0N/A * @return the number of elements in this list
0N/A */
0N/A public int size() {
0N/A return size;
0N/A }
0N/A
0N/A /**
0N/A * Returns <tt>true</tt> if this list contains no elements.
0N/A *
0N/A * @return <tt>true</tt> if this list contains no elements
0N/A */
0N/A public boolean isEmpty() {
0N/A return size == 0;
0N/A }
0N/A
0N/A /**
0N/A * Returns <tt>true</tt> if this list contains the specified element.
0N/A * More formally, returns <tt>true</tt> if and only if this list contains
0N/A * at least one element <tt>e</tt> such that
0N/A * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
0N/A *
0N/A * @param o element whose presence in this list is to be tested
0N/A * @return <tt>true</tt> if this list contains the specified element
0N/A */
0N/A public boolean contains(Object o) {
0N/A return indexOf(o) >= 0;
0N/A }
0N/A
0N/A /**
0N/A * Returns the index of the first occurrence of the specified element
0N/A * in this list, or -1 if this list does not contain the element.
0N/A * More formally, returns the lowest index <tt>i</tt> such that
0N/A * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
0N/A * or -1 if there is no such index.
0N/A */
0N/A public int indexOf(Object o) {
0N/A if (o == null) {
0N/A for (int i = 0; i < size; i++)
0N/A if (elementData[i]==null)
0N/A return i;
0N/A } else {
0N/A for (int i = 0; i < size; i++)
0N/A if (o.equals(elementData[i]))
0N/A return i;
0N/A }
0N/A return -1;
0N/A }
0N/A
0N/A /**
0N/A * Returns the index of the last occurrence of the specified element
0N/A * in this list, or -1 if this list does not contain the element.
0N/A * More formally, returns the highest index <tt>i</tt> such that
0N/A * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
0N/A * or -1 if there is no such index.
0N/A */
0N/A public int lastIndexOf(Object o) {
0N/A if (o == null) {
0N/A for (int i = size-1; i >= 0; i--)
0N/A if (elementData[i]==null)
0N/A return i;
0N/A } else {
0N/A for (int i = size-1; i >= 0; i--)
0N/A if (o.equals(elementData[i]))
0N/A return i;
0N/A }
0N/A return -1;
0N/A }
0N/A
0N/A /**
0N/A * Returns a shallow copy of this <tt>ArrayList</tt> instance. (The
0N/A * elements themselves are not copied.)
0N/A *
0N/A * @return a clone of this <tt>ArrayList</tt> instance
0N/A */
0N/A public Object clone() {
0N/A try {
0N/A @SuppressWarnings("unchecked")
0N/A ArrayList<E> v = (ArrayList<E>) super.clone();
0N/A v.elementData = Arrays.copyOf(elementData, size);
0N/A v.modCount = 0;
0N/A return v;
0N/A } catch (CloneNotSupportedException e) {
0N/A // this shouldn't happen, since we are Cloneable
0N/A throw new InternalError();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns an array containing all of the elements in this list
0N/A * in proper sequence (from first to last element).
0N/A *
0N/A * <p>The returned array will be "safe" in that no references to it are
0N/A * maintained by this list. (In other words, this method must allocate
0N/A * a new array). The caller is thus free to modify the returned array.
0N/A *
0N/A * <p>This method acts as bridge between array-based and collection-based
0N/A * APIs.
0N/A *
0N/A * @return an array containing all of the elements in this list in
0N/A * proper sequence
0N/A */
0N/A public Object[] toArray() {
0N/A return Arrays.copyOf(elementData, size);
0N/A }
0N/A
0N/A /**
0N/A * Returns an array containing all of the elements in this list in proper
0N/A * sequence (from first to last element); the runtime type of the returned
0N/A * array is that of the specified array. If the list fits in the
0N/A * specified array, it is returned therein. Otherwise, a new array is
0N/A * allocated with the runtime type of the specified array and the size of
0N/A * this list.
0N/A *
0N/A * <p>If the list fits in the specified array with room to spare
0N/A * (i.e., the array has more elements than the list), the element in
0N/A * the array immediately following the end of the collection is set to
0N/A * <tt>null</tt>. (This is useful in determining the length of the
0N/A * list <i>only</i> if the caller knows that the list does not contain
0N/A * any null elements.)
0N/A *
0N/A * @param a the array into which the elements of the list are to
0N/A * be stored, if it is big enough; otherwise, a new array of the
0N/A * same runtime type is allocated for this purpose.
0N/A * @return an array containing the elements of the list
0N/A * @throws ArrayStoreException if the runtime type of the specified array
0N/A * is not a supertype of the runtime type of every element in
0N/A * this list
0N/A * @throws NullPointerException if the specified array is null
0N/A */
0N/A @SuppressWarnings("unchecked")
0N/A public <T> T[] toArray(T[] a) {
0N/A if (a.length < size)
0N/A // Make a new array of a's runtime type, but my contents:
0N/A return (T[]) Arrays.copyOf(elementData, size, a.getClass());
0N/A System.arraycopy(elementData, 0, a, 0, size);
0N/A if (a.length > size)
0N/A a[size] = null;
0N/A return a;
0N/A }
0N/A
0N/A // Positional Access Operations
0N/A
0N/A @SuppressWarnings("unchecked")
0N/A E elementData(int index) {
0N/A return (E) elementData[index];
0N/A }
0N/A
0N/A /**
0N/A * Returns the element at the specified position in this list.
0N/A *
0N/A * @param index index of the element to return
0N/A * @return the element at the specified position in this list
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public E get(int index) {
0N/A rangeCheck(index);
0N/A
0N/A return elementData(index);
0N/A }
0N/A
0N/A /**
0N/A * Replaces the element at the specified position in this list with
0N/A * the specified element.
0N/A *
0N/A * @param index index of the element to replace
0N/A * @param element element to be stored at the specified position
0N/A * @return the element previously at the specified position
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public E set(int index, E element) {
0N/A rangeCheck(index);
0N/A
0N/A E oldValue = elementData(index);
0N/A elementData[index] = element;
0N/A return oldValue;
0N/A }
0N/A
0N/A /**
0N/A * Appends the specified element to the end of this list.
0N/A *
0N/A * @param e element to be appended to this list
0N/A * @return <tt>true</tt> (as specified by {@link Collection#add})
0N/A */
0N/A public boolean add(E e) {
2979N/A ensureCapacityInternal(size + 1); // Increments modCount!!
0N/A elementData[size++] = e;
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Inserts the specified element at the specified position in this
0N/A * list. Shifts the element currently at that position (if any) and
0N/A * any subsequent elements to the right (adds one to their indices).
0N/A *
0N/A * @param index index at which the specified element is to be inserted
0N/A * @param element element to be inserted
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public void add(int index, E element) {
0N/A rangeCheckForAdd(index);
0N/A
2979N/A ensureCapacityInternal(size + 1); // Increments modCount!!
0N/A System.arraycopy(elementData, index, elementData, index + 1,
0N/A size - index);
0N/A elementData[index] = element;
0N/A size++;
0N/A }
0N/A
0N/A /**
0N/A * Removes the element at the specified position in this list.
0N/A * Shifts any subsequent elements to the left (subtracts one from their
0N/A * indices).
0N/A *
0N/A * @param index the index of the element to be removed
0N/A * @return the element that was removed from the list
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public E remove(int index) {
0N/A rangeCheck(index);
0N/A
0N/A modCount++;
0N/A E oldValue = elementData(index);
0N/A
0N/A int numMoved = size - index - 1;
0N/A if (numMoved > 0)
0N/A System.arraycopy(elementData, index+1, elementData, index,
0N/A numMoved);
6061N/A elementData[--size] = null; // clear to let GC do its work
0N/A
0N/A return oldValue;
0N/A }
0N/A
0N/A /**
0N/A * Removes the first occurrence of the specified element from this list,
0N/A * if it is present. If the list does not contain the element, it is
0N/A * unchanged. More formally, removes the element with the lowest index
0N/A * <tt>i</tt> such that
0N/A * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>
0N/A * (if such an element exists). Returns <tt>true</tt> if this list
0N/A * contained the specified element (or equivalently, if this list
0N/A * changed as a result of the call).
0N/A *
0N/A * @param o element to be removed from this list, if present
0N/A * @return <tt>true</tt> if this list contained the specified element
0N/A */
0N/A public boolean remove(Object o) {
0N/A if (o == null) {
0N/A for (int index = 0; index < size; index++)
0N/A if (elementData[index] == null) {
0N/A fastRemove(index);
0N/A return true;
0N/A }
0N/A } else {
0N/A for (int index = 0; index < size; index++)
0N/A if (o.equals(elementData[index])) {
0N/A fastRemove(index);
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /*
0N/A * Private remove method that skips bounds checking and does not
0N/A * return the value removed.
0N/A */
0N/A private void fastRemove(int index) {
0N/A modCount++;
0N/A int numMoved = size - index - 1;
0N/A if (numMoved > 0)
0N/A System.arraycopy(elementData, index+1, elementData, index,
0N/A numMoved);
6061N/A elementData[--size] = null; // clear to let GC do its work
0N/A }
0N/A
0N/A /**
0N/A * Removes all of the elements from this list. The list will
0N/A * be empty after this call returns.
0N/A */
0N/A public void clear() {
0N/A modCount++;
0N/A
6061N/A // clear to let GC do its work
0N/A for (int i = 0; i < size; i++)
0N/A elementData[i] = null;
0N/A
0N/A size = 0;
0N/A }
0N/A
0N/A /**
0N/A * Appends all of the elements in the specified collection to the end of
0N/A * this list, in the order that they are returned by the
0N/A * specified collection's Iterator. The behavior of this operation is
0N/A * undefined if the specified collection is modified while the operation
0N/A * is in progress. (This implies that the behavior of this call is
0N/A * undefined if the specified collection is this list, and this
0N/A * list is nonempty.)
0N/A *
0N/A * @param c collection containing elements to be added to this list
0N/A * @return <tt>true</tt> if this list changed as a result of the call
0N/A * @throws NullPointerException if the specified collection is null
0N/A */
0N/A public boolean addAll(Collection<? extends E> c) {
0N/A Object[] a = c.toArray();
0N/A int numNew = a.length;
2979N/A ensureCapacityInternal(size + numNew); // Increments modCount
0N/A System.arraycopy(a, 0, elementData, size, numNew);
0N/A size += numNew;
0N/A return numNew != 0;
0N/A }
0N/A
0N/A /**
0N/A * Inserts all of the elements in the specified collection into this
0N/A * list, starting at the specified position. Shifts the element
0N/A * currently at that position (if any) and any subsequent elements to
0N/A * the right (increases their indices). The new elements will appear
0N/A * in the list in the order that they are returned by the
0N/A * specified collection's iterator.
0N/A *
0N/A * @param index index at which to insert the first element from the
0N/A * specified collection
0N/A * @param c collection containing elements to be added to this list
0N/A * @return <tt>true</tt> if this list changed as a result of the call
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A * @throws NullPointerException if the specified collection is null
0N/A */
0N/A public boolean addAll(int index, Collection<? extends E> c) {
0N/A rangeCheckForAdd(index);
0N/A
0N/A Object[] a = c.toArray();
0N/A int numNew = a.length;
2979N/A ensureCapacityInternal(size + numNew); // Increments modCount
0N/A
0N/A int numMoved = size - index;
0N/A if (numMoved > 0)
0N/A System.arraycopy(elementData, index, elementData, index + numNew,
0N/A numMoved);
0N/A
0N/A System.arraycopy(a, 0, elementData, index, numNew);
0N/A size += numNew;
0N/A return numNew != 0;
0N/A }
0N/A
0N/A /**
0N/A * Removes from this list all of the elements whose index is between
0N/A * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
0N/A * Shifts any succeeding elements to the left (reduces their index).
0N/A * This call shortens the list by {@code (toIndex - fromIndex)} elements.
0N/A * (If {@code toIndex==fromIndex}, this operation has no effect.)
0N/A *
0N/A * @throws IndexOutOfBoundsException if {@code fromIndex} or
0N/A * {@code toIndex} is out of range
0N/A * ({@code fromIndex < 0 ||
0N/A * fromIndex >= size() ||
0N/A * toIndex > size() ||
0N/A * toIndex < fromIndex})
0N/A */
0N/A protected void removeRange(int fromIndex, int toIndex) {
0N/A modCount++;
0N/A int numMoved = size - toIndex;
0N/A System.arraycopy(elementData, toIndex, elementData, fromIndex,
0N/A numMoved);
0N/A
6061N/A // clear to let GC do its work
0N/A int newSize = size - (toIndex-fromIndex);
6061N/A for (int i = newSize; i < size; i++) {
6061N/A elementData[i] = null;
6061N/A }
6061N/A size = newSize;
0N/A }
0N/A
0N/A /**
0N/A * Checks if the given index is in range. If not, throws an appropriate
0N/A * runtime exception. This method does *not* check if the index is
0N/A * negative: It is always used immediately prior to an array access,
0N/A * which throws an ArrayIndexOutOfBoundsException if index is negative.
0N/A */
0N/A private void rangeCheck(int index) {
0N/A if (index >= size)
0N/A throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
0N/A }
0N/A
0N/A /**
0N/A * A version of rangeCheck used by add and addAll.
0N/A */
0N/A private void rangeCheckForAdd(int index) {
0N/A if (index > size || index < 0)
0N/A throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
0N/A }
0N/A
0N/A /**
0N/A * Constructs an IndexOutOfBoundsException detail message.
0N/A * Of the many possible refactorings of the error handling code,
0N/A * this "outlining" performs best with both server and client VMs.
0N/A */
0N/A private String outOfBoundsMsg(int index) {
0N/A return "Index: "+index+", Size: "+size;
0N/A }
0N/A
0N/A /**
0N/A * Removes from this list all of its elements that are contained in the
0N/A * specified collection.
0N/A *
0N/A * @param c collection containing elements to be removed from this list
0N/A * @return {@code true} if this list changed as a result of the call
0N/A * @throws ClassCastException if the class of an element of this list
4106N/A * is incompatible with the specified collection
4106N/A * (<a href="Collection.html#optional-restrictions">optional</a>)
0N/A * @throws NullPointerException if this list contains a null element and the
4106N/A * specified collection does not permit null elements
4106N/A * (<a href="Collection.html#optional-restrictions">optional</a>),
0N/A * or if the specified collection is null
0N/A * @see Collection#contains(Object)
0N/A */
0N/A public boolean removeAll(Collection<?> c) {
0N/A return batchRemove(c, false);
0N/A }
0N/A
0N/A /**
0N/A * Retains only the elements in this list that are contained in the
0N/A * specified collection. In other words, removes from this list all
0N/A * of its elements that are not contained in the specified collection.
0N/A *
0N/A * @param c collection containing elements to be retained in this list
0N/A * @return {@code true} if this list changed as a result of the call
0N/A * @throws ClassCastException if the class of an element of this list
4106N/A * is incompatible with the specified collection
4106N/A * (<a href="Collection.html#optional-restrictions">optional</a>)
0N/A * @throws NullPointerException if this list contains a null element and the
4106N/A * specified collection does not permit null elements
4106N/A * (<a href="Collection.html#optional-restrictions">optional</a>),
0N/A * or if the specified collection is null
0N/A * @see Collection#contains(Object)
0N/A */
0N/A public boolean retainAll(Collection<?> c) {
0N/A return batchRemove(c, true);
0N/A }
0N/A
0N/A private boolean batchRemove(Collection<?> c, boolean complement) {
0N/A final Object[] elementData = this.elementData;
0N/A int r = 0, w = 0;
0N/A boolean modified = false;
0N/A try {
0N/A for (; r < size; r++)
0N/A if (c.contains(elementData[r]) == complement)
0N/A elementData[w++] = elementData[r];
0N/A } finally {
0N/A // Preserve behavioral compatibility with AbstractCollection,
0N/A // even if c.contains() throws.
0N/A if (r != size) {
0N/A System.arraycopy(elementData, r,
0N/A elementData, w,
0N/A size - r);
0N/A w += size - r;
0N/A }
0N/A if (w != size) {
6061N/A // clear to let GC do its work
0N/A for (int i = w; i < size; i++)
0N/A elementData[i] = null;
0N/A modCount += size - w;
0N/A size = w;
0N/A modified = true;
0N/A }
0N/A }
0N/A return modified;
0N/A }
0N/A
0N/A /**
0N/A * Save the state of the <tt>ArrayList</tt> instance to a stream (that
0N/A * is, serialize it).
0N/A *
0N/A * @serialData The length of the array backing the <tt>ArrayList</tt>
0N/A * instance is emitted (int), followed by all of its elements
0N/A * (each an <tt>Object</tt>) in the proper order.
0N/A */
0N/A private void writeObject(java.io.ObjectOutputStream s)
0N/A throws java.io.IOException{
0N/A // Write out element count, and any hidden stuff
0N/A int expectedModCount = modCount;
0N/A s.defaultWriteObject();
0N/A
6061N/A // Write out size as capacity for behavioural compatibility with clone()
6061N/A s.writeInt(size);
0N/A
0N/A // Write out all elements in the proper order.
6061N/A for (int i=0; i<size; i++) {
0N/A s.writeObject(elementData[i]);
6061N/A }
0N/A
0N/A if (modCount != expectedModCount) {
0N/A throw new ConcurrentModificationException();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Reconstitute the <tt>ArrayList</tt> instance from a stream (that is,
0N/A * deserialize it).
0N/A */
0N/A private void readObject(java.io.ObjectInputStream s)
0N/A throws java.io.IOException, ClassNotFoundException {
6061N/A elementData = EMPTY_ELEMENTDATA;
6061N/A
0N/A // Read in size, and any hidden stuff
0N/A s.defaultReadObject();
0N/A
6061N/A // Read in capacity
6061N/A s.readInt(); // ignored
6061N/A
6061N/A if (size > 0) {
6061N/A // be like clone(), allocate array based upon size not capacity
6061N/A ensureCapacityInternal(size);
0N/A
6061N/A Object[] a = elementData;
6061N/A // Read in all elements in the proper order.
6061N/A for (int i=0; i<size; i++) {
6061N/A a[i] = s.readObject();
6061N/A }
6061N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a list iterator over the elements in this list (in proper
0N/A * sequence), starting at the specified position in the list.
0N/A * The specified index indicates the first element that would be
0N/A * returned by an initial call to {@link ListIterator#next next}.
0N/A * An initial call to {@link ListIterator#previous previous} would
0N/A * return the element with the specified index minus one.
0N/A *
0N/A * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
0N/A *
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public ListIterator<E> listIterator(int index) {
0N/A if (index < 0 || index > size)
0N/A throw new IndexOutOfBoundsException("Index: "+index);
0N/A return new ListItr(index);
0N/A }
0N/A
0N/A /**
0N/A * Returns a list iterator over the elements in this list (in proper
0N/A * sequence).
0N/A *
0N/A * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
0N/A *
0N/A * @see #listIterator(int)
0N/A */
0N/A public ListIterator<E> listIterator() {
0N/A return new ListItr(0);
0N/A }
0N/A
0N/A /**
0N/A * Returns an iterator over the elements in this list in proper sequence.
0N/A *
0N/A * <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
0N/A *
0N/A * @return an iterator over the elements in this list in proper sequence
0N/A */
0N/A public Iterator<E> iterator() {
0N/A return new Itr();
0N/A }
0N/A
0N/A /**
0N/A * An optimized version of AbstractList.Itr
0N/A */
0N/A private class Itr implements Iterator<E> {
0N/A int cursor; // index of next element to return
0N/A int lastRet = -1; // index of last element returned; -1 if no such
0N/A int expectedModCount = modCount;
0N/A
0N/A public boolean hasNext() {
0N/A return cursor != size;
0N/A }
0N/A
0N/A @SuppressWarnings("unchecked")
0N/A public E next() {
0N/A checkForComodification();
0N/A int i = cursor;
0N/A if (i >= size)
0N/A throw new NoSuchElementException();
0N/A Object[] elementData = ArrayList.this.elementData;
0N/A if (i >= elementData.length)
0N/A throw new ConcurrentModificationException();
0N/A cursor = i + 1;
0N/A return (E) elementData[lastRet = i];
0N/A }
0N/A
0N/A public void remove() {
0N/A if (lastRet < 0)
0N/A throw new IllegalStateException();
0N/A checkForComodification();
0N/A
0N/A try {
0N/A ArrayList.this.remove(lastRet);
0N/A cursor = lastRet;
0N/A lastRet = -1;
0N/A expectedModCount = modCount;
0N/A } catch (IndexOutOfBoundsException ex) {
0N/A throw new ConcurrentModificationException();
0N/A }
0N/A }
0N/A
0N/A final void checkForComodification() {
0N/A if (modCount != expectedModCount)
0N/A throw new ConcurrentModificationException();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * An optimized version of AbstractList.ListItr
0N/A */
0N/A private class ListItr extends Itr implements ListIterator<E> {
0N/A ListItr(int index) {
0N/A super();
0N/A cursor = index;
0N/A }
0N/A
0N/A public boolean hasPrevious() {
0N/A return cursor != 0;
0N/A }
0N/A
0N/A public int nextIndex() {
0N/A return cursor;
0N/A }
0N/A
0N/A public int previousIndex() {
0N/A return cursor - 1;
0N/A }
0N/A
0N/A @SuppressWarnings("unchecked")
0N/A public E previous() {
0N/A checkForComodification();
0N/A int i = cursor - 1;
0N/A if (i < 0)
0N/A throw new NoSuchElementException();
0N/A Object[] elementData = ArrayList.this.elementData;
0N/A if (i >= elementData.length)
0N/A throw new ConcurrentModificationException();
0N/A cursor = i;
0N/A return (E) elementData[lastRet = i];
0N/A }
0N/A
0N/A public void set(E e) {
0N/A if (lastRet < 0)
0N/A throw new IllegalStateException();
0N/A checkForComodification();
0N/A
0N/A try {
0N/A ArrayList.this.set(lastRet, e);
0N/A } catch (IndexOutOfBoundsException ex) {
0N/A throw new ConcurrentModificationException();
0N/A }
0N/A }
0N/A
0N/A public void add(E e) {
0N/A checkForComodification();
0N/A
0N/A try {
0N/A int i = cursor;
0N/A ArrayList.this.add(i, e);
0N/A cursor = i + 1;
0N/A lastRet = -1;
0N/A expectedModCount = modCount;
0N/A } catch (IndexOutOfBoundsException ex) {
0N/A throw new ConcurrentModificationException();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a view of the portion of this list between the specified
0N/A * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. (If
0N/A * {@code fromIndex} and {@code toIndex} are equal, the returned list is
0N/A * empty.) The returned list is backed by this list, so non-structural
0N/A * changes in the returned list are reflected in this list, and vice-versa.
0N/A * The returned list supports all of the optional list operations.
0N/A *
0N/A * <p>This method eliminates the need for explicit range operations (of
0N/A * the sort that commonly exist for arrays). Any operation that expects
0N/A * a list can be used as a range operation by passing a subList view
0N/A * instead of a whole list. For example, the following idiom
0N/A * removes a range of elements from a list:
0N/A * <pre>
0N/A * list.subList(from, to).clear();
0N/A * </pre>
0N/A * Similar idioms may be constructed for {@link #indexOf(Object)} and
0N/A * {@link #lastIndexOf(Object)}, and all of the algorithms in the
0N/A * {@link Collections} class can be applied to a subList.
0N/A *
0N/A * <p>The semantics of the list returned by this method become undefined if
0N/A * the backing list (i.e., this list) is <i>structurally modified</i> in
0N/A * any way other than via the returned list. (Structural modifications are
0N/A * those that change the size of this list, or otherwise perturb it in such
0N/A * a fashion that iterations in progress may yield incorrect results.)
0N/A *
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A * @throws IllegalArgumentException {@inheritDoc}
0N/A */
0N/A public List<E> subList(int fromIndex, int toIndex) {
0N/A subListRangeCheck(fromIndex, toIndex, size);
0N/A return new SubList(this, 0, fromIndex, toIndex);
0N/A }
0N/A
0N/A static void subListRangeCheck(int fromIndex, int toIndex, int size) {
0N/A if (fromIndex < 0)
0N/A throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
0N/A if (toIndex > size)
0N/A throw new IndexOutOfBoundsException("toIndex = " + toIndex);
0N/A if (fromIndex > toIndex)
0N/A throw new IllegalArgumentException("fromIndex(" + fromIndex +
0N/A ") > toIndex(" + toIndex + ")");
0N/A }
0N/A
0N/A private class SubList extends AbstractList<E> implements RandomAccess {
0N/A private final AbstractList<E> parent;
0N/A private final int parentOffset;
0N/A private final int offset;
28N/A int size;
0N/A
0N/A SubList(AbstractList<E> parent,
0N/A int offset, int fromIndex, int toIndex) {
0N/A this.parent = parent;
0N/A this.parentOffset = fromIndex;
0N/A this.offset = offset + fromIndex;
0N/A this.size = toIndex - fromIndex;
0N/A this.modCount = ArrayList.this.modCount;
0N/A }
0N/A
0N/A public E set(int index, E e) {
0N/A rangeCheck(index);
0N/A checkForComodification();
0N/A E oldValue = ArrayList.this.elementData(offset + index);
0N/A ArrayList.this.elementData[offset + index] = e;
0N/A return oldValue;
0N/A }
0N/A
0N/A public E get(int index) {
0N/A rangeCheck(index);
0N/A checkForComodification();
0N/A return ArrayList.this.elementData(offset + index);
0N/A }
0N/A
0N/A public int size() {
0N/A checkForComodification();
0N/A return this.size;
0N/A }
0N/A
0N/A public void add(int index, E e) {
0N/A rangeCheckForAdd(index);
0N/A checkForComodification();
0N/A parent.add(parentOffset + index, e);
0N/A this.modCount = parent.modCount;
0N/A this.size++;
0N/A }
0N/A
0N/A public E remove(int index) {
0N/A rangeCheck(index);
0N/A checkForComodification();
0N/A E result = parent.remove(parentOffset + index);
0N/A this.modCount = parent.modCount;
0N/A this.size--;
0N/A return result;
0N/A }
0N/A
0N/A protected void removeRange(int fromIndex, int toIndex) {
0N/A checkForComodification();
0N/A parent.removeRange(parentOffset + fromIndex,
0N/A parentOffset + toIndex);
0N/A this.modCount = parent.modCount;
0N/A this.size -= toIndex - fromIndex;
0N/A }
0N/A
0N/A public boolean addAll(Collection<? extends E> c) {
0N/A return addAll(this.size, c);
0N/A }
0N/A
0N/A public boolean addAll(int index, Collection<? extends E> c) {
0N/A rangeCheckForAdd(index);
0N/A int cSize = c.size();
0N/A if (cSize==0)
0N/A return false;
0N/A
0N/A checkForComodification();
0N/A parent.addAll(parentOffset + index, c);
0N/A this.modCount = parent.modCount;
0N/A this.size += cSize;
0N/A return true;
0N/A }
0N/A
0N/A public Iterator<E> iterator() {
0N/A return listIterator();
0N/A }
0N/A
0N/A public ListIterator<E> listIterator(final int index) {
0N/A checkForComodification();
0N/A rangeCheckForAdd(index);
28N/A final int offset = this.offset;
0N/A
0N/A return new ListIterator<E>() {
0N/A int cursor = index;
0N/A int lastRet = -1;
0N/A int expectedModCount = ArrayList.this.modCount;
0N/A
0N/A public boolean hasNext() {
0N/A return cursor != SubList.this.size;
0N/A }
0N/A
0N/A @SuppressWarnings("unchecked")
0N/A public E next() {
0N/A checkForComodification();
0N/A int i = cursor;
0N/A if (i >= SubList.this.size)
0N/A throw new NoSuchElementException();
0N/A Object[] elementData = ArrayList.this.elementData;
0N/A if (offset + i >= elementData.length)
0N/A throw new ConcurrentModificationException();
0N/A cursor = i + 1;
0N/A return (E) elementData[offset + (lastRet = i)];
0N/A }
0N/A
0N/A public boolean hasPrevious() {
0N/A return cursor != 0;
0N/A }
0N/A
0N/A @SuppressWarnings("unchecked")
0N/A public E previous() {
0N/A checkForComodification();
0N/A int i = cursor - 1;
0N/A if (i < 0)
0N/A throw new NoSuchElementException();
0N/A Object[] elementData = ArrayList.this.elementData;
0N/A if (offset + i >= elementData.length)
0N/A throw new ConcurrentModificationException();
0N/A cursor = i;
0N/A return (E) elementData[offset + (lastRet = i)];
0N/A }
0N/A
0N/A public int nextIndex() {
0N/A return cursor;
0N/A }
0N/A
0N/A public int previousIndex() {
0N/A return cursor - 1;
0N/A }
0N/A
0N/A public void remove() {
0N/A if (lastRet < 0)
0N/A throw new IllegalStateException();
0N/A checkForComodification();
0N/A
0N/A try {
0N/A SubList.this.remove(lastRet);
0N/A cursor = lastRet;
0N/A lastRet = -1;
0N/A expectedModCount = ArrayList.this.modCount;
0N/A } catch (IndexOutOfBoundsException ex) {
0N/A throw new ConcurrentModificationException();
0N/A }
0N/A }
0N/A
0N/A public void set(E e) {
0N/A if (lastRet < 0)
0N/A throw new IllegalStateException();
0N/A checkForComodification();
0N/A
0N/A try {
0N/A ArrayList.this.set(offset + lastRet, e);
0N/A } catch (IndexOutOfBoundsException ex) {
0N/A throw new ConcurrentModificationException();
0N/A }
0N/A }
0N/A
0N/A public void add(E e) {
0N/A checkForComodification();
0N/A
0N/A try {
0N/A int i = cursor;
0N/A SubList.this.add(i, e);
0N/A cursor = i + 1;
0N/A lastRet = -1;
0N/A expectedModCount = ArrayList.this.modCount;
0N/A } catch (IndexOutOfBoundsException ex) {
0N/A throw new ConcurrentModificationException();
0N/A }
0N/A }
0N/A
0N/A final void checkForComodification() {
0N/A if (expectedModCount != ArrayList.this.modCount)
0N/A throw new ConcurrentModificationException();
0N/A }
0N/A };
0N/A }
0N/A
0N/A public List<E> subList(int fromIndex, int toIndex) {
0N/A subListRangeCheck(fromIndex, toIndex, size);
0N/A return new SubList(this, offset, fromIndex, toIndex);
0N/A }
0N/A
0N/A private void rangeCheck(int index) {
0N/A if (index < 0 || index >= this.size)
0N/A throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
0N/A }
0N/A
0N/A private void rangeCheckForAdd(int index) {
0N/A if (index < 0 || index > this.size)
0N/A throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
0N/A }
0N/A
0N/A private String outOfBoundsMsg(int index) {
0N/A return "Index: "+index+", Size: "+this.size;
0N/A }
0N/A
0N/A private void checkForComodification() {
0N/A if (ArrayList.this.modCount != this.modCount)
0N/A throw new ConcurrentModificationException();
0N/A }
0N/A }
0N/A}