Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    *
    * This code is free software; you can redistribute it and/or modify it
    * under the terms of the GNU General Public License version 2 only, as
    * published by the Free Software Foundation.  Sun designates this
    * particular file as subject to the "Classpath" exception as provided
    * by Sun in the LICENSE file that accompanied this code.
   *
   * This code is distributed in the hope that it will be useful, but WITHOUT
   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   * version 2 for more details (a copy is included in the LICENSE file that
   * accompanied this code).
   *
   * You should have received a copy of the GNU General Public License version
   * 2 along with this work; if not, write to the Free Software Foundation,
   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   *
   * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
   * CA 95054 USA or visit www.sun.com if you need additional information or
   * have any questions.
   */
  
  package java.util;

Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. (This class is roughly equivalent to Vector, except that it is unsynchronized.)

The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation.

Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.

An application can increase the capacity of an ArrayList instance before adding a large number of elements using the ensureCapacity operation. This may reduce the amount of incremental reallocation.

Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the Collections.synchronizedList method. This is best done at creation time, to prevent accidental unsynchronized access to the list:

   List list = Collections.synchronizedList(new ArrayList(...));

The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.

This class is a member of the Java Collections Framework.

Author(s):
Josh Bloch
Neal Gafter
Since:
1.2
See also:
Collection
List
LinkedList
Vector

 
 
 public class ArrayList<E> extends AbstractList<E>
         implements List<E>, RandomAccessCloneablejava.io.Serializable
 {
     private static final long serialVersionUID = 8683452581122892189L;

    
The array buffer into which the elements of the ArrayList are stored. The capacity of the ArrayList is the length of this array buffer.
 
     private transient Object[] elementData;

    
The size of the ArrayList (the number of elements it contains).

Serial:
 
     private int size;

    
Constructs an empty list with the specified initial capacity.

Parameters:
initialCapacity the initial capacity of the list
Throws:
java.lang.IllegalArgumentException if the specified initial capacity is negative
 
     public ArrayList(int initialCapacity) {
         super();
         if (initialCapacity < 0)
             throw new IllegalArgumentException("Illegal Capacity: "+
                                                initialCapacity);
         this. = new Object[initialCapacity];
     }

    
Constructs an empty list with an initial capacity of ten.
 
     public ArrayList() {
         this(10);
     }

    
Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

Parameters:
c the collection whose elements are to be placed into this list
Throws:
java.lang.NullPointerException if the specified collection is null
 
     public ArrayList(Collection<? extends E> c) {
          = c.toArray();
          = .;
         // c.toArray might (incorrectly) not return Object[] (see 6260652)
         if (.getClass() != Object[].class)
              = Arrays.copyOf(Object[].class);
     }

    
Trims the capacity of this ArrayList instance to be the list's current size. An application can use this operation to minimize the storage of an ArrayList instance.
 
     public void trimToSize() {
         ++;
         int oldCapacity = .;
         if ( < oldCapacity) {
              = Arrays.copyOf();
         }
     }

    
Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.

Parameters:
minCapacity the desired minimum capacity
 
     public void ensureCapacity(int minCapacity) {
         ++;
         int oldCapacity = .;
         if (minCapacity > oldCapacity) {
             Object oldData[] = ;
             int newCapacity = (oldCapacity * 3)/2 + 1;
             if (newCapacity < minCapacity)
                 newCapacity = minCapacity;
             // minCapacity is usually close to size, so this is a win:
              = Arrays.copyOf(newCapacity);
         }
     }

    
Returns the number of elements in this list.

Returns:
the number of elements in this list
 
     public int size() {
         return ;
     }

    
Returns true if this list contains no elements.

Returns:
true if this list contains no elements
 
     public boolean isEmpty() {
         return  == 0;
     }

    
Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).

Parameters:
o element whose presence in this list is to be tested
Returns:
true if this list contains the specified element
 
     public boolean contains(Object o) {
         return indexOf(o) >= 0;
     }

    
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.
 
     public int indexOf(Object o) {
         if (o == null) {
             for (int i = 0; i < i++)
                 if ([i]==null)
                     return i;
         } else {
             for (int i = 0; i < i++)
                 if (o.equals([i]))
                     return i;
         }
         return -1;
     }

    
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the highest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.
 
     public int lastIndexOf(Object o) {
         if (o == null) {
             for (int i = -1; i >= 0; i--)
                 if ([i]==null)
                     return i;
         } else {
             for (int i = -1; i >= 0; i--)
                 if (o.equals([i]))
                     return i;
         }
         return -1;
     }

    
Returns a shallow copy of this ArrayList instance. (The elements themselves are not copied.)

Returns:
a clone of this ArrayList instance
 
     public Object clone() {
         try {
             @SuppressWarnings("unchecked")
                 ArrayList<E> v = (ArrayList<E>) super.clone();
             v.elementData = Arrays.copyOf();
             v.modCount = 0;
             return v;
         } catch (CloneNotSupportedException e) {
             // this shouldn't happen, since we are Cloneable
             throw new InternalError();
         }
     }

    
Returns an array containing all of the elements in this list in proper sequence (from first to last element).

The returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.

This method acts as bridge between array-based and collection-based APIs.

Returns:
an array containing all of the elements in this list in proper sequence
 
     public Object[] toArray() {
         return Arrays.copyOf();
     }

    
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.

If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the collection is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)

Parameters:
a the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Returns:
an array containing the elements of the list
Throws:
java.lang.ArrayStoreException if the runtime type of the specified array is not a supertype of the runtime type of every element in this list
java.lang.NullPointerException if the specified array is null
 
     @SuppressWarnings("unchecked")
     public <T> T[] toArray(T[] a) {
         if (a.length < )
             // Make a new array of a's runtime type, but my contents:
             return (T[]) Arrays.copyOf(a.getClass());
         System.arraycopy(, 0, a, 0, );
         if (a.length > )
             a[] = null;
         return a;
     }
 
     // Positional Access Operations
 
     @SuppressWarnings("unchecked")
     E elementData(int index) {
         return (E) [index];
     }

    
Returns the element at the specified position in this list.

Parameters:
index index of the element to return
Returns:
the element at the specified position in this list
Throws:
java.lang.IndexOutOfBoundsException
 
     public E get(int index) {
         rangeCheck(index);
 
         return elementData(index);
     }

    
Replaces the element at the specified position in this list with the specified element.

Parameters:
index index of the element to replace
element element to be stored at the specified position
Returns:
the element previously at the specified position
Throws:
java.lang.IndexOutOfBoundsException
 
     public E set(int index, E element) {
         rangeCheck(index);
 
         E oldValue = elementData(index);
         [index] = element;
         return oldValue;
     }

    
Appends the specified element to the end of this list.

Parameters:
e element to be appended to this list
Returns:
true (as specified by Collection.add(java.lang.Object))
 
     public boolean add(E e) {
         ensureCapacity( + 1);  // Increments modCount!!
         [++] = e;
         return true;
     }

    
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Parameters:
index index at which the specified element is to be inserted
element element to be inserted
Throws:
java.lang.IndexOutOfBoundsException
 
     public void add(int index, E element) {
         rangeCheckForAdd(index);
 
         ensureCapacity(+1);  // Increments modCount!!
         System.arraycopy(indexindex + 1,
                           - index);
         [index] = element;
         ++;
     }

    
Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

Parameters:
index the index of the element to be removed
Returns:
the element that was removed from the list
Throws:
java.lang.IndexOutOfBoundsException
 
     public E remove(int index) {
         rangeCheck(index);
 
         ++;
         E oldValue = elementData(index);
 
         int numMoved =  - index - 1;
         if (numMoved > 0)
             System.arraycopy(index+1, index,
                              numMoved);
         [--] = null// Let gc do its work
 
         return oldValue;
     }

    
Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).

Parameters:
o element to be removed from this list, if present
Returns:
true if this list contained the specified element
 
     public boolean remove(Object o) {
         if (o == null) {
             for (int index = 0; index < index++)
                 if ([index] == null) {
                     fastRemove(index);
                     return true;
                 }
         } else {
             for (int index = 0; index < index++)
                 if (o.equals([index])) {
                     fastRemove(index);
                     return true;
                 }
         }
         return false;
     }
 
     /*
      * Private remove method that skips bounds checking and does not
      * return the value removed.
      */
     private void fastRemove(int index) {
         ++;
         int numMoved =  - index - 1;
         if (numMoved > 0)
             System.arraycopy(index+1, index,
                              numMoved);
         [--] = null// Let gc do its work
     }

    
Removes all of the elements from this list. The list will be empty after this call returns.
 
     public void clear() {
         ++;
 
         // Let gc do its work
         for (int i = 0; i < i++)
             [i] = null;
 
          = 0;
     }

    
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.)

Parameters:
c collection containing elements to be added to this list
Returns:
true if this list changed as a result of the call
Throws:
java.lang.NullPointerException if the specified collection is null
 
     public boolean addAll(Collection<? extends E> c) {
         Object[] a = c.toArray();
         int numNew = a.length;
         ensureCapacity( + numNew);  // Increments modCount
         System.arraycopy(a, 0, numNew);
          += numNew;
         return numNew != 0;
     }

    
Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that they are returned by the specified collection's iterator.

Parameters:
index index at which to insert the first element from the specified collection
c collection containing elements to be added to this list
Returns:
true if this list changed as a result of the call
Throws:
java.lang.IndexOutOfBoundsException
java.lang.NullPointerException if the specified collection is null
 
     public boolean addAll(int indexCollection<? extends E> c) {
         rangeCheckForAdd(index);
 
         Object[] a = c.toArray();
         int numNew = a.length;
         ensureCapacity( + numNew);  // Increments modCount
 
         int numMoved =  - index;
         if (numMoved > 0)
             System.arraycopy(indexindex + numNew,
                              numMoved);
 
         System.arraycopy(a, 0, indexnumNew);
          += numNew;
         return numNew != 0;
     }

    
Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by (toIndex - fromIndex) elements. (If toIndex==fromIndex, this operation has no effect.)

Throws:
java.lang.IndexOutOfBoundsException if fromIndex or toIndex is out of range (fromIndex < 0 || fromIndex >= size() || toIndex > size() || toIndex < fromIndex)
 
     protected void removeRange(int fromIndexint toIndex) {
         ++;
         int numMoved =  - toIndex;
         System.arraycopy(toIndexfromIndex,
                          numMoved);
 
         // Let gc do its work
         int newSize =  - (toIndex-fromIndex);
         while ( != newSize)
             [--] = null;
     }

    
Checks if the given index is in range. If not, throws an appropriate runtime exception. This method does *not* check if the index is negative: It is always used immediately prior to an array access, which throws an ArrayIndexOutOfBoundsException if index is negative.
 
     private void rangeCheck(int index) {
         if (index >= )
             throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
     }

    
A version of rangeCheck used by add and addAll.
 
     private void rangeCheckForAdd(int index) {
         if (index >  || index < 0)
             throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
     }

    
Constructs an IndexOutOfBoundsException detail message. Of the many possible refactorings of the error handling code, this "outlining" performs best with both server and client VMs.
 
     private String outOfBoundsMsg(int index) {
         return "Index: "+index+", Size: "+;
     }

    
Removes from this list all of its elements that are contained in the specified collection.

Parameters:
c collection containing elements to be removed from this list
Returns:
true if this list changed as a result of the call
Throws:
java.lang.ClassCastException if the class of an element of this list is incompatible with the specified collection (optional)
java.lang.NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null
See also:
Collection.contains(java.lang.Object)
 
     public boolean removeAll(Collection<?> c) {
         return batchRemove(cfalse);
     }

    
Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection.

Parameters:
c collection containing elements to be retained in this list
Returns:
true if this list changed as a result of the call
Throws:
java.lang.ClassCastException if the class of an element of this list is incompatible with the specified collection (optional)
java.lang.NullPointerException if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null
See also:
Collection.contains(java.lang.Object)
 
     public boolean retainAll(Collection<?> c) {
         return batchRemove(ctrue);
     }
 
     private boolean batchRemove(Collection<?> cboolean complement) {
         final Object[] elementData = this.;
         int r = 0, w = 0;
         boolean modified = false;
         try {
             for (; r < r++)
                 if (c.contains(elementData[r]) == complement)
                     elementData[w++] = elementData[r];
         } finally {
             // Preserve behavioral compatibility with AbstractCollection,
             // even if c.contains() throws.
             if (r != ) {
                 System.arraycopy(elementDatar,
                                  elementDataw,
                                   - r);
                 w +=  - r;
             }
             if (w != ) {
                 for (int i = wi < i++)
                     elementData[i] = null;
                  +=  - w;
                  = w;
                 modified = true;
             }
         }
         return modified;
     }

    
Save the state of the ArrayList instance to a stream (that is, serialize it).

SerialData:
The length of the array backing the ArrayList instance is emitted (int), followed by all of its elements (each an Object) in the proper order.
 
     private void writeObject(java.io.ObjectOutputStream s)
         throws java.io.IOException{
         // Write out element count, and any hidden stuff
         int expectedModCount = ;
         s.defaultWriteObject();
 
         // Write out array length
         s.writeInt(.);
 
         // Write out all elements in the proper order.
         for (int i=0; i<i++)
             s.writeObject([i]);
 
         if ( != expectedModCount) {
             throw new ConcurrentModificationException();
         }
 
     }

    
Reconstitute the ArrayList instance from a stream (that is, deserialize it).
 
     private void readObject(java.io.ObjectInputStream s)
         throws java.io.IOExceptionClassNotFoundException {
         // Read in size, and any hidden stuff
         s.defaultReadObject();
 
         // Read in array length and allocate array
         int arrayLength = s.readInt();
         Object[] a =  = new Object[arrayLength];
 
         // Read in all elements in the proper order.
         for (int i=0; i<i++)
             a[i] = s.readObject();
     }

    
Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. The specified index indicates the first element that would be returned by an initial call to next. An initial call to previous would return the element with the specified index minus one.

The returned list iterator is fail-fast.

 
     public ListIterator<E> listIterator(int index) {
         if (index < 0 || index > )
             throw new IndexOutOfBoundsException("Index: "+index);
         return new ListItr(index);
     }

    
Returns a list iterator over the elements in this list (in proper sequence).

The returned list iterator is fail-fast.

 
     public ListIterator<E> listIterator() {
         return new ListItr(0);
     }

    
Returns an iterator over the elements in this list in proper sequence.

The returned iterator is fail-fast.

Returns:
an iterator over the elements in this list in proper sequence
 
     public Iterator<E> iterator() {
         return new Itr();
     }

    
An optimized version of AbstractList.Itr
 
     private class Itr implements Iterator<E> {
         int cursor;       // index of next element to return
         int lastRet = -1; // index of last element returned; -1 if no such
         int expectedModCount = ;
 
         public boolean hasNext() {
             return  != ;
         }
 
         @SuppressWarnings("unchecked")
         public E next() {
             checkForComodification();
             int i = ;
             if (i >= )
                 throw new NoSuchElementException();
             Object[] elementData = ArrayList.this.;
             if (i >= elementData.length)
                 throw new ConcurrentModificationException();
              = i + 1;
             return (E) elementData[ = i];
         }
 
         public void remove() {
             if ( < 0)
                 throw new IllegalStateException();
             checkForComodification();
 
             try {
                 ArrayList.this.remove();
                  = ;
                  = -1;
                  = ;
             } catch (IndexOutOfBoundsException ex) {
                 throw new ConcurrentModificationException();
             }
         }
 
         final void checkForComodification() {
             if ( != )
                 throw new ConcurrentModificationException();
         }
     }

    
An optimized version of AbstractList.ListItr
 
     private class ListItr extends Itr implements ListIterator<E> {
         ListItr(int index) {
             super();
              = index;
         }
 
         public boolean hasPrevious() {
             return  != 0;
         }
 
         public int nextIndex() {
             return ;
         }
 
         public int previousIndex() {
             return  - 1;
         }
 
         @SuppressWarnings("unchecked")
         public E previous() {
             checkForComodification();
             int i =  - 1;
             if (i < 0)
                 throw new NoSuchElementException();
             Object[] elementData = ArrayList.this.;
             if (i >= elementData.length)
                 throw new ConcurrentModificationException();
              = i;
             return (E) elementData[ = i];
         }
 
         public void set(E e) {
             if ( < 0)
                 throw new IllegalStateException();
             checkForComodification();
 
             try {
                 ArrayList.this.set(e);
             } catch (IndexOutOfBoundsException ex) {
                 throw new ConcurrentModificationException();
             }
         }
 
         public void add(E e) {
             checkForComodification();
 
             try {
                 int i = ;
                 ArrayList.this.add(ie);
                  = i + 1;
                  = -1;
                  = ;
             } catch (IndexOutOfBoundsException ex) {
                 throw new ConcurrentModificationException();
             }
         }
     }

    
Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations.

This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:

      list.subList(from, to).clear();
 
Similar idioms may be constructed for indexOf(java.lang.Object) and lastIndexOf(java.lang.Object), and all of the algorithms in the Collections class can be applied to a subList.

The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)

 
     public List<E> subList(int fromIndexint toIndex) {
         subListRangeCheck(fromIndextoIndex);
         return new SubList(this, 0, fromIndextoIndex);
     }
 
     static void subListRangeCheck(int fromIndexint toIndexint size) {
         if (fromIndex < 0)
             throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
         if (toIndex > size)
             throw new IndexOutOfBoundsException("toIndex = " + toIndex);
         if (fromIndex > toIndex)
             throw new IllegalArgumentException("fromIndex(" + fromIndex +
                                                ") > toIndex(" + toIndex + ")");
     }
 
     private class SubList extends AbstractList<E> implements RandomAccess {
         private final AbstractList<E> parent;
         private final int parentOffset;
         private final int offset;
         private int size;
 
         SubList(AbstractList<E> parent,
                 int offsetint fromIndexint toIndex) {
             this. = parent;
             this. = fromIndex;
             this. = offset + fromIndex;
             this. = toIndex - fromIndex;
             this. = ArrayList.this.;
         }
 
         public E set(int index, E e) {
             rangeCheck(index);
             checkForComodification();
             E oldValue = ArrayList.this.elementData( + index);
             ArrayList.this.[ + index] = e;
             return oldValue;
         }
 
         public E get(int index) {
             rangeCheck(index);
             checkForComodification();
             return ArrayList.this.elementData( + index);
         }
 
         public int size() {
             checkForComodification();
             return this.;
         }
 
         public void add(int index, E e) {
             rangeCheckForAdd(index);
             checkForComodification();
             .add( + indexe);
             this. = .;
             this.++;
         }
 
         public E remove(int index) {
             rangeCheck(index);
             checkForComodification();
             E result = .remove( + index);
             this. = .;
             this.--;
             return result;
         }
 
         protected void removeRange(int fromIndexint toIndex) {
             checkForComodification();
             .removeRange( + fromIndex,
                                 + toIndex);
             this. = .;
             this. -= toIndex - fromIndex;
         }
 
         public boolean addAll(Collection<? extends E> c) {
             return addAll(this.c);
         }
 
         public boolean addAll(int indexCollection<? extends E> c) {
             rangeCheckForAdd(index);
             int cSize = c.size();
             if (cSize==0)
                 return false;
 
             checkForComodification();
             .addAll( + indexc);
             this. = .;
             this. += cSize;
             return true;
         }
 
         public Iterator<E> iterator() {
             return listIterator();
         }
 
         public ListIterator<E> listIterator(final int index) {
             checkForComodification();
             rangeCheckForAdd(index);
 
             return new ListIterator<E>() {
                 int cursor = index;
                 int lastRet = -1;
                 int expectedModCount = ArrayList.this.;
 
                 public boolean hasNext() {
                     return  != SubList.this.;
                 }
 
                 @SuppressWarnings("unchecked")
                 public E next() {
                     checkForComodification();
                     int i = ;
                     if (i >= SubList.this.)
                         throw new NoSuchElementException();
                     Object[] elementData = ArrayList.this.;
                     if ( + i >= elementData.length)
                         throw new ConcurrentModificationException();
                      = i + 1;
                     return (E) elementData[ + ( = i)];
                 }
 
                 public boolean hasPrevious() {
                     return  != 0;
                 }
                @SuppressWarnings("unchecked")
                public E previous() {
                    checkForComodification();
                    int i =  - 1;
                    if (i < 0)
                        throw new NoSuchElementException();
                    Object[] elementData = ArrayList.this.;
                    if ( + i >= elementData.length)
                        throw new ConcurrentModificationException();
                     = i;
                    return (E) elementData[ + ( = i)];
                }
                public int nextIndex() {
                    return ;
                }
                public int previousIndex() {
                    return  - 1;
                }
                public void remove() {
                    if ( < 0)
                        throw new IllegalStateException();
                    checkForComodification();
                    try {
                        SubList.this.remove();
                         = ;
                         = -1;
                         = ArrayList.this.;
                    } catch (IndexOutOfBoundsException ex) {
                        throw new ConcurrentModificationException();
                    }
                }
                public void set(E e) {
                    if ( < 0)
                        throw new IllegalStateException();
                    checkForComodification();
                    try {
                        ArrayList.this.set( + e);
                    } catch (IndexOutOfBoundsException ex) {
                        throw new ConcurrentModificationException();
                    }
                }
                public void add(E e) {
                    checkForComodification();
                    try {
                        int i = ;
                        SubList.this.add(ie);
                         = i + 1;
                         = -1;
                         = ArrayList.this.;
                    } catch (IndexOutOfBoundsException ex) {
                        throw new ConcurrentModificationException();
                    }
                }
                final void checkForComodification() {
                    if ( != ArrayList.this.)
                        throw new ConcurrentModificationException();
                }
            };
        }
        public List<E> subList(int fromIndexint toIndex) {
            subListRangeCheck(fromIndextoIndex);
            return new SubList(thisfromIndextoIndex);
        }
        private void rangeCheck(int index) {
            if (index < 0 || index >= this.)
                throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
        }
        private void rangeCheckForAdd(int index) {
            if (index < 0 || index > this.)
                throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
        }
        private String outOfBoundsMsg(int index) {
            return "Index: "+index+", Size: "+this.;
        }
        private void checkForComodification() {
            if (ArrayList.this. != this.)
                throw new ConcurrentModificationException();
        }
    }
New to GrepCode? Check out our FAQ X