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 * 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 * 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 * 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. 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 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 * <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 * <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 * <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 * <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 * 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 * <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 * <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 * <p>This class is a member of the 0N/A * Java Collections Framework</a>. 0N/A * @author Josh Bloch 0N/A * @author Neal Gafter 6061N/A * Default initial capacity. 6061N/A * Shared empty array instance used for empty instances. 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 * The size of the ArrayList (the number of elements it contains). 0N/A * Constructs an empty list with the specified initial capacity. 3203N/A * @param initialCapacity the initial capacity of the list 3203N/A * @throws IllegalArgumentException if the specified initial capacity 0N/A * Constructs an empty list with an initial capacity of ten. 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 * @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 // c.toArray might (incorrectly) not return Object[] (see 6260652) 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 * 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. 3203N/A * @param minCapacity the desired minimum capacity 6061N/A // any size if real element table 6061N/A // larger than default for empty table. It's already supposed to be 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 * 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 * @param minCapacity the desired minimum capacity 2350N/A // minCapacity is usually close to size, so this is a win: 0N/A * Returns the number of elements in this list. 0N/A * @return the number of elements in this list 0N/A * Returns <tt>true</tt> if this list contains no elements. 0N/A * @return <tt>true</tt> if this list contains no elements 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 ? e==null : o.equals(e))</tt>. 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 * 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 ? get(i)==null : o.equals(get(i)))</tt>, 0N/A * or -1 if there is no such index. 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 ? get(i)==null : o.equals(get(i)))</tt>, 0N/A * or -1 if there is no such index. 0N/A * Returns a shallow copy of this <tt>ArrayList</tt> instance. (The 0N/A * elements themselves are not copied.) 0N/A * @return a clone of this <tt>ArrayList</tt> instance 0N/A // this shouldn't happen, since we are Cloneable 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 * <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 * <p>This method acts as bridge between array-based and collection-based 0N/A * @return an array containing all of the elements in this list in 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 * <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 * @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 * @throws NullPointerException if the specified array is null 0N/A // Make a new array of a's runtime type, but my contents: 0N/A // Positional Access Operations 0N/A * Returns the element at the specified position in this list. 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 * Replaces the element at the specified position in this list with 0N/A * the specified element. 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 * Appends the specified element to the end of this list. 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 * 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 * @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 * 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 * @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 * 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 ? get(i)==null : 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 * @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 * Private remove method that skips bounds checking and does not 0N/A * return the value removed. 0N/A * Removes all of the elements from this list. The list will 0N/A * be empty after this call returns. 6061N/A // clear to let GC do its work 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 * @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 * 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 * @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 * 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 * @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}) 6061N/A // clear to let GC do its work 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 * A version of rangeCheck used by add and addAll. 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 * Removes from this list all of its elements that are contained in the 0N/A * specified collection. 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 0N/A * @throws NullPointerException if this list contains a null element and the 4106N/A * specified collection does not permit null elements 0N/A * or if the specified collection is null 0N/A * @see Collection#contains(Object) 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 * @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 0N/A * @throws NullPointerException if this list contains a null element and the 4106N/A * specified collection does not permit null elements 0N/A * or if the specified collection is null 0N/A * @see Collection#contains(Object) 0N/A // Preserve behavioral compatibility with AbstractCollection, 0N/A // even if c.contains() throws. 6061N/A // clear to let GC do its work 0N/A * Save the state of the <tt>ArrayList</tt> instance to a stream (that 0N/A * is, serialize it). 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 // Write out element count, and any hidden stuff 6061N/A // Write out size as capacity for behavioural compatibility with clone() 0N/A // Write out all elements in the proper order. 0N/A * Reconstitute the <tt>ArrayList</tt> instance from a stream (that is, 0N/A // Read in size, and any hidden stuff 6061N/A // be like clone(), allocate array based upon size not capacity 6061N/A // Read in all elements in the proper order. 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 * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>. 0N/A * @throws IndexOutOfBoundsException {@inheritDoc} 0N/A * Returns a list iterator over the elements in this list (in proper 0N/A * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>. 0N/A * @see #listIterator(int) 0N/A * Returns an iterator over the elements in this list in proper sequence. 0N/A * <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>. 0N/A * @return an iterator over the elements in this list in proper sequence 0N/A * An optimized version of AbstractList.Itr 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 * An optimized version of AbstractList.ListItr 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 * <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 * list.subList(from, to).clear(); 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 * <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 * @throws IndexOutOfBoundsException {@inheritDoc} 0N/A * @throws IllegalArgumentException {@inheritDoc}