ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/ArrayList.java
Revision: 1.6
Committed: Sat Nov 26 04:35:16 2005 UTC (18 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.5: +1 -1 lines
Log Message:
style

File Contents

# User Rev Content
1 dl 1.1 /*
2     * %W% %E%
3     *
4     * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
5     * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6     */
7    
8     package java.util;
9     import java.util.*; // for javadoc (till 6280605 is fixed)
10    
11     /**
12     * Resizable-array implementation of the <tt>List</tt> interface. Implements
13     * all optional list operations, and permits all elements, including
14     * <tt>null</tt>. In addition to implementing the <tt>List</tt> interface,
15     * this class provides methods to manipulate the size of the array that is
16     * used internally to store the list. (This class is roughly equivalent to
17     * <tt>Vector</tt>, except that it is unsynchronized.)<p>
18     *
19     * The <tt>size</tt>, <tt>isEmpty</tt>, <tt>get</tt>, <tt>set</tt>,
20     * <tt>iterator</tt>, and <tt>listIterator</tt> operations run in constant
21     * time. The <tt>add</tt> operation runs in <i>amortized constant time</i>,
22     * that is, adding n elements requires O(n) time. All of the other operations
23     * run in linear time (roughly speaking). The constant factor is low compared
24     * to that for the <tt>LinkedList</tt> implementation.<p>
25     *
26     * Each <tt>ArrayList</tt> instance has a <i>capacity</i>. The capacity is
27     * the size of the array used to store the elements in the list. It is always
28     * at least as large as the list size. As elements are added to an ArrayList,
29     * its capacity grows automatically. The details of the growth policy are not
30     * specified beyond the fact that adding an element has constant amortized
31     * time cost.<p>
32     *
33     * An application can increase the capacity of an <tt>ArrayList</tt> instance
34     * before adding a large number of elements using the <tt>ensureCapacity</tt>
35     * operation. This may reduce the amount of incremental reallocation.
36     *
37     * <p><strong>Note that this implementation is not synchronized.</strong>
38     * If multiple threads access an <tt>ArrayList</tt> instance concurrently,
39     * and at least one of the threads modifies the list structurally, it
40     * <i>must</i> be synchronized externally. (A structural modification is
41     * any operation that adds or deletes one or more elements, or explicitly
42     * resizes the backing array; merely setting the value of an element is not
43     * a structural modification.) This is typically accomplished by
44     * synchronizing on some object that naturally encapsulates the list.
45     *
46     * If no such object exists, the list should be "wrapped" using the
47     * {@link Collections#synchronizedList Collections.synchronizedList}
48     * method. This is best done at creation time, to prevent accidental
49     * unsynchronized access to the list:<pre>
50     * List list = Collections.synchronizedList(new ArrayList(...));</pre>
51     *
52     * <p>The iterators returned by this class's <tt>iterator</tt> and
53     * <tt>listIterator</tt> methods are <i>fail-fast</i>: if the list is
54     * structurally modified at any time after the iterator is created, in any way
55     * except through the iterator's own <tt>remove</tt> or <tt>add</tt> methods,
56     * the iterator will throw a {@link ConcurrentModificationException}. Thus, in
57     * the face of concurrent modification, the iterator fails quickly and cleanly,
58     * rather than risking arbitrary, non-deterministic behavior at an undetermined
59     * time in the future.<p>
60     *
61     * Note that the fail-fast behavior of an iterator cannot be guaranteed
62     * as it is, generally speaking, impossible to make any hard guarantees in the
63     * presence of unsynchronized concurrent modification. Fail-fast iterators
64     * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
65     * Therefore, it would be wrong to write a program that depended on this
66     * exception for its correctness: <i>the fail-fast behavior of iterators
67     * should be used only to detect bugs.</i><p>
68     *
69     * This class is a member of the
70     * <a href="{@docRoot}/../guide/collections/index.html">
71     * Java Collections Framework</a>.
72     *
73     * @author Josh Bloch
74     * @author Neal Gafter
75     * @version %I%, %G%
76     * @see Collection
77     * @see List
78     * @see LinkedList
79     * @see Vector
80     * @since 1.2
81     */
82    
83     public class ArrayList<E> extends AbstractList<E>
84     implements List<E>, RandomAccess, Cloneable, java.io.Serializable
85     {
86     private static final long serialVersionUID = 8683452581122892189L;
87    
88     /**
89     * The array buffer into which the elements of the ArrayList are stored.
90     * The capacity of the ArrayList is the length of this array buffer.
91     */
92     private transient Object[] elementData;
93    
94     /**
95     * The size of the ArrayList (the number of elements it contains).
96     *
97     * @serial
98     */
99     private int size;
100    
101     /**
102     * Constructs an empty list with the specified initial capacity.
103     *
104 jsr166 1.5 * @param initialCapacity the initial capacity of the list
105     * @throws IllegalArgumentException if the specified initial capacity
106     * is negative
107 dl 1.1 */
108     public ArrayList(int initialCapacity) {
109     super();
110     if (initialCapacity < 0)
111     throw new IllegalArgumentException("Illegal Capacity: "+
112     initialCapacity);
113     this.elementData = new Object[initialCapacity];
114     }
115    
116     /**
117     * Constructs an empty list with an initial capacity of ten.
118     */
119     public ArrayList() {
120     this(10);
121     }
122    
123     /**
124     * Constructs a list containing the elements of the specified
125     * collection, in the order they are returned by the collection's
126 dl 1.2 * iterator. The <tt>ArrayList</tt> instance has an initial capacity of
127     * 110% the size of the specified collection.
128 dl 1.1 *
129     * @param c the collection whose elements are to be placed into this list
130     * @throws NullPointerException if the specified collection is null
131     */
132     public ArrayList(Collection<? extends E> c) {
133 dl 1.2 int size = c.size();
134     // 10% for growth
135     int cap = ((size/10)+1)*11;
136     if (cap > 0) {
137     Object[] a = new Object[cap];
138     a[size] = a[size+1] = UNALLOCATED;
139     Object[] b = c.toArray(a);
140     if (b[size] == null && b[size+1] == UNALLOCATED) {
141     b[size+1] = null;
142     elementData = b;
143     this.size = size;
144     return;
145     }
146     }
147     initFromConcurrentlyMutating(c);
148     }
149 jsr166 1.4
150 dl 1.2 private void initFromConcurrentlyMutating(Collection<? extends E> c) {
151     elementData = c.toArray();
152     size = elementData.length;
153     // c.toArray might (incorrectly) not return Object[] (see 6260652)
154     if (elementData.getClass() != Object[].class)
155     elementData = Arrays.copyOf(elementData, size, Object[].class);
156     }
157 jsr166 1.4
158 dl 1.2 private final static Object UNALLOCATED = new Object();
159 jsr166 1.4
160 dl 1.1 /**
161     * Trims the capacity of this <tt>ArrayList</tt> instance to be the
162     * list's current size. An application can use this operation to minimize
163     * the storage of an <tt>ArrayList</tt> instance.
164     */
165     public void trimToSize() {
166     modCount++;
167     int oldCapacity = elementData.length;
168     if (size < oldCapacity) {
169     elementData = Arrays.copyOf(elementData, size);
170     }
171     }
172    
173     /**
174     * Increases the capacity of this <tt>ArrayList</tt> instance, if
175     * necessary, to ensure that it can hold at least the number of elements
176     * specified by the minimum capacity argument.
177     *
178 jsr166 1.5 * @param minCapacity the desired minimum capacity
179 dl 1.1 */
180     public void ensureCapacity(int minCapacity) {
181     modCount++;
182     if (minCapacity > elementData.length)
183     growArray(minCapacity);
184     }
185    
186     /**
187 jsr166 1.5 * Increases the capacity of the array.
188     *
189     * @param minCapacity the desired minimum capacity
190 dl 1.1 */
191     private void growArray(int minCapacity) {
192     int oldCapacity = elementData.length;
193     // Double size if small; else grow by 50%
194 jsr166 1.4 int newCapacity = ((oldCapacity < 64)?
195 dl 1.1 (oldCapacity * 2):
196     ((oldCapacity * 3)/2 + 1));
197     if (newCapacity < minCapacity)
198     newCapacity = minCapacity;
199     elementData = Arrays.copyOf(elementData, newCapacity);
200     }
201    
202     /**
203     * Returns the number of elements in this list.
204     *
205     * @return the number of elements in this list
206     */
207     public int size() {
208     return size;
209     }
210    
211     /**
212     * Returns <tt>true</tt> if this list contains no elements.
213     *
214     * @return <tt>true</tt> if this list contains no elements
215     */
216     public boolean isEmpty() {
217     return size == 0;
218     }
219    
220     /**
221     * Returns <tt>true</tt> if this list contains the specified element.
222     * More formally, returns <tt>true</tt> if and only if this list contains
223     * at least one element <tt>e</tt> such that
224     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
225     *
226     * @param o element whose presence in this list is to be tested
227     * @return <tt>true</tt> if this list contains the specified element
228     */
229     public boolean contains(Object o) {
230     return indexOf(o) >= 0;
231     }
232    
233     /**
234     * Returns the index of the first occurrence of the specified element
235     * in this list, or -1 if this list does not contain the element.
236     * More formally, returns the lowest index <tt>i</tt> such that
237     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
238     * or -1 if there is no such index.
239     */
240     public int indexOf(Object o) {
241     if (o == null) {
242     for (int i = 0; i < size; i++)
243     if (elementData[i]==null)
244     return i;
245     } else {
246     for (int i = 0; i < size; i++)
247     if (o.equals(elementData[i]))
248     return i;
249     }
250     return -1;
251     }
252    
253     /**
254     * Returns the index of the last occurrence of the specified element
255     * in this list, or -1 if this list does not contain the element.
256     * More formally, returns the highest index <tt>i</tt> such that
257     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
258     * or -1 if there is no such index.
259     */
260     public int lastIndexOf(Object o) {
261     if (o == null) {
262     for (int i = size-1; i >= 0; i--)
263     if (elementData[i]==null)
264     return i;
265     } else {
266     for (int i = size-1; i >= 0; i--)
267     if (o.equals(elementData[i]))
268     return i;
269     }
270     return -1;
271     }
272    
273     /**
274     * Returns a shallow copy of this <tt>ArrayList</tt> instance. (The
275     * elements themselves are not copied.)
276     *
277     * @return a clone of this <tt>ArrayList</tt> instance
278     */
279     public Object clone() {
280     try {
281     ArrayList<E> v = (ArrayList<E>) super.clone();
282     v.elementData = Arrays.copyOf(elementData, size);
283     v.modCount = 0;
284     return v;
285     } catch (CloneNotSupportedException e) {
286     // this shouldn't happen, since we are Cloneable
287     throw new InternalError();
288     }
289     }
290    
291     /**
292     * Returns an array containing all of the elements in this list
293     * in proper sequence (from first to last element).
294     *
295     * <p>The returned array will be "safe" in that no references to it are
296     * maintained by this list. (In other words, this method must allocate
297     * a new array). The caller is thus free to modify the returned array.
298     *
299     * <p>This method acts as bridge between array-based and collection-based
300     * APIs.
301     *
302     * @return an array containing all of the elements in this list in
303     * proper sequence
304     */
305     public Object[] toArray() {
306     return Arrays.copyOf(elementData, size);
307     }
308    
309     /**
310     * Returns an array containing all of the elements in this list in proper
311     * sequence (from first to last element); the runtime type of the returned
312     * array is that of the specified array. If the list fits in the
313     * specified array, it is returned therein. Otherwise, a new array is
314     * allocated with the runtime type of the specified array and the size of
315     * this list.
316     *
317     * <p>If the list fits in the specified array with room to spare
318     * (i.e., the array has more elements than the list), the element in
319     * the array immediately following the end of the collection is set to
320     * <tt>null</tt>. (This is useful in determining the length of the
321     * list <i>only</i> if the caller knows that the list does not contain
322     * any null elements.)
323     *
324     * @param a the array into which the elements of the list are to
325     * be stored, if it is big enough; otherwise, a new array of the
326     * same runtime type is allocated for this purpose.
327     * @return an array containing the elements of the list
328     * @throws ArrayStoreException if the runtime type of the specified array
329     * is not a supertype of the runtime type of every element in
330     * this list
331     * @throws NullPointerException if the specified array is null
332     */
333     public <T> T[] toArray(T[] a) {
334     if (a.length < size)
335     // Make a new array of a's runtime type, but my contents:
336     return (T[]) Arrays.copyOf(elementData, size, a.getClass());
337     System.arraycopy(elementData, 0, a, 0, size);
338     if (a.length > size)
339     a[size] = null;
340     return a;
341     }
342    
343     // Positional Access Operations
344    
345 jsr166 1.4 /**
346 jsr166 1.6 * Creates and returns an appropriate exception for indexing errors.
347 dl 1.1 */
348     private static IndexOutOfBoundsException rangeException(int i, int s) {
349     return new IndexOutOfBoundsException("Index: " + i + ", Size: " + s);
350     }
351    
352     /**
353     * Returns the element at the specified position in this list.
354     *
355     * @param index index of the element to return
356     * @return the element at the specified position in this list
357     * @throws IndexOutOfBoundsException {@inheritDoc}
358     */
359     public E get(int index) {
360     if (index >= size)
361     throw rangeException(index, size);
362     return (E)elementData[index];
363     }
364    
365     /**
366     * Replaces the element at the specified position in this list with
367     * the specified element.
368     *
369     * @param index index of the element to replace
370     * @param element element to be stored at the specified position
371     * @return the element previously at the specified position
372     * @throws IndexOutOfBoundsException {@inheritDoc}
373     */
374     public E set(int index, E element) {
375     if (index >= size)
376     throw rangeException(index, size);
377    
378     E oldValue = (E) elementData[index];
379     elementData[index] = element;
380     return oldValue;
381     }
382    
383     /**
384     * Appends the specified element to the end of this list.
385     *
386     * @param e element to be appended to this list
387     * @return <tt>true</tt> (as specified by {@link Collection#add})
388     */
389     public boolean add(E e) {
390     ++modCount;
391     int s = size++;
392     if (s >= elementData.length)
393     growArray(s + 1);
394     elementData[s] = e;
395     return true;
396     }
397    
398     /**
399     * Inserts the specified element at the specified position in this
400     * list. Shifts the element currently at that position (if any) and
401     * any subsequent elements to the right (adds one to their indices).
402     *
403     * @param index index at which the specified element is to be inserted
404     * @param element element to be inserted
405     * @throws IndexOutOfBoundsException {@inheritDoc}
406     */
407     public void add(int index, E element) {
408     int s = size;
409     if (index > s || index < 0)
410     throw rangeException(index, s);
411     ++modCount;
412     size = s + 1;
413     if (s >= elementData.length)
414     growArray(s + 1);
415     System.arraycopy(elementData, index, elementData, index + 1,
416     s - index);
417     elementData[index] = element;
418     }
419    
420     /**
421     * Removes the element at the specified position in this list.
422     * Shifts any subsequent elements to the left (subtracts one from their
423     * indices).
424     *
425     * @param index the index of the element to be removed
426     * @return the element that was removed from the list
427     * @throws IndexOutOfBoundsException {@inheritDoc}
428     */
429     public E remove(int index) {
430     int s = size - 1;
431     if (index > s)
432     throw rangeException(index, size);
433     size = s;
434     modCount++;
435     Object oldValue = elementData[index];
436     int numMoved = s - index;
437     if (numMoved > 0)
438 jsr166 1.4 System.arraycopy(elementData, index+1, elementData, index,
439 dl 1.1 numMoved);
440     elementData[s] = null; // forget removed element
441     return (E)oldValue;
442     }
443    
444     /**
445     * Removes the first occurrence of the specified element from this list,
446     * if it is present. If the list does not contain the element, it is
447     * unchanged. More formally, removes the element with the lowest index
448     * <tt>i</tt> such that
449     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>
450     * (if such an element exists). Returns <tt>true</tt> if this list
451     * contained the specified element (or equivalently, if this list
452     * changed as a result of the call).
453     *
454     * @param o element to be removed from this list, if present
455     * @return <tt>true</tt> if this list contained the specified element
456     */
457     public boolean remove(Object o) {
458     if (o == null) {
459     for (int index = 0; index < size; index++)
460     if (elementData[index] == null) {
461     fastRemove(index);
462     return true;
463     }
464     } else {
465     for (int index = 0; index < size; index++)
466     if (o.equals(elementData[index])) {
467     fastRemove(index);
468     return true;
469     }
470     }
471     return false;
472     }
473    
474     /*
475     * Private remove method that skips bounds checking and does not
476     * return the value removed.
477     */
478     private void fastRemove(int index) {
479     modCount++;
480     int numMoved = size - index - 1;
481     if (numMoved > 0)
482     System.arraycopy(elementData, index+1, elementData, index,
483     numMoved);
484     elementData[--size] = null; // Let gc do its work
485     }
486    
487     /**
488     * Removes all of the elements from this list. The list will
489     * be empty after this call returns.
490     */
491     public void clear() {
492     modCount++;
493    
494     // Let gc do its work
495     for (int i = 0; i < size; i++)
496     elementData[i] = null;
497    
498     size = 0;
499     }
500    
501     /**
502     * Appends all of the elements in the specified collection to the end of
503     * this list, in the order that they are returned by the
504     * specified collection's Iterator. The behavior of this operation is
505     * undefined if the specified collection is modified while the operation
506     * is in progress. (This implies that the behavior of this call is
507     * undefined if the specified collection is this list, and this
508     * list is nonempty.)
509     *
510     * @param c collection containing elements to be added to this list
511     * @return <tt>true</tt> if this list changed as a result of the call
512     * @throws NullPointerException if the specified collection is null
513     */
514     public boolean addAll(Collection<? extends E> c) {
515     Object[] a = c.toArray();
516     int numNew = a.length;
517     ensureCapacity(size + numNew); // Increments modCount
518     System.arraycopy(a, 0, elementData, size, numNew);
519     size += numNew;
520     return numNew != 0;
521     }
522    
523     /**
524     * Inserts all of the elements in the specified collection into this
525     * list, starting at the specified position. Shifts the element
526     * currently at that position (if any) and any subsequent elements to
527     * the right (increases their indices). The new elements will appear
528     * in the list in the order that they are returned by the
529     * specified collection's iterator.
530     *
531     * @param index index at which to insert the first element from the
532     * specified collection
533     * @param c collection containing elements to be added to this list
534     * @return <tt>true</tt> if this list changed as a result of the call
535     * @throws IndexOutOfBoundsException {@inheritDoc}
536     * @throws NullPointerException if the specified collection is null
537     */
538     public boolean addAll(int index, Collection<? extends E> c) {
539     if (index > size || index < 0)
540     throw new IndexOutOfBoundsException(
541     "Index: " + index + ", Size: " + size);
542    
543     Object[] a = c.toArray();
544     int numNew = a.length;
545     ensureCapacity(size + numNew); // Increments modCount
546    
547     int numMoved = size - index;
548     if (numMoved > 0)
549     System.arraycopy(elementData, index, elementData, index + numNew,
550     numMoved);
551    
552     System.arraycopy(a, 0, elementData, index, numNew);
553     size += numNew;
554     return numNew != 0;
555     }
556    
557     /**
558     * Removes from this list all of the elements whose index is between
559     * <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive.
560     * Shifts any succeeding elements to the left (reduces their index).
561     * This call shortens the list by <tt>(toIndex - fromIndex)</tt> elements.
562     * (If <tt>toIndex==fromIndex</tt>, this operation has no effect.)
563     *
564     * @param fromIndex index of first element to be removed
565     * @param toIndex index after last element to be removed
566     * @throws IndexOutOfBoundsException if fromIndex or toIndex out of
567     * range (fromIndex &lt; 0 || fromIndex &gt;= size() || toIndex
568     * &gt; size() || toIndex &lt; fromIndex)
569     */
570     protected void removeRange(int fromIndex, int toIndex) {
571     modCount++;
572     int numMoved = size - toIndex;
573     System.arraycopy(elementData, toIndex, elementData, fromIndex,
574     numMoved);
575    
576     // Let gc do its work
577     int newSize = size - (toIndex-fromIndex);
578     while (size != newSize)
579     elementData[--size] = null;
580     }
581    
582     /**
583     * Save the state of the <tt>ArrayList</tt> instance to a stream (that
584     * is, serialize it).
585     *
586     * @serialData The length of the array backing the <tt>ArrayList</tt>
587     * instance is emitted (int), followed by all of its elements
588     * (each an <tt>Object</tt>) in the proper order.
589     */
590     private void writeObject(java.io.ObjectOutputStream s)
591     throws java.io.IOException{
592     // Write out element count, and any hidden stuff
593     int expectedModCount = modCount;
594     s.defaultWriteObject();
595    
596     // Write out array length
597     s.writeInt(elementData.length);
598    
599     // Write out all elements in the proper order.
600     for (int i=0; i<size; i++)
601     s.writeObject(elementData[i]);
602    
603     if (modCount != expectedModCount) {
604     throw new ConcurrentModificationException();
605     }
606    
607     }
608    
609     /**
610     * Reconstitute the <tt>ArrayList</tt> instance from a stream (that is,
611     * deserialize it).
612     */
613     private void readObject(java.io.ObjectInputStream s)
614     throws java.io.IOException, ClassNotFoundException {
615     // Read in size, and any hidden stuff
616     s.defaultReadObject();
617    
618     // Read in array length and allocate array
619     int arrayLength = s.readInt();
620     Object[] a = elementData = new Object[arrayLength];
621    
622     // Read in all elements in the proper order.
623     for (int i=0; i<size; i++)
624     a[i] = s.readObject();
625     }
626    
627    
628     /**
629     * Returns a list-iterator of the elements in this list (in proper
630     * sequence), starting at the specified position in the list.
631     * Obeys the general contract of <tt>List.listIterator(int)</tt>.<p>
632     *
633     * The list-iterator is <i>fail-fast</i>: if the list is structurally
634     * modified at any time after the Iterator is created, in any way except
635     * through the list-iterator's own <tt>remove</tt> or <tt>add</tt>
636     * methods, the list-iterator will throw a
637     * <tt>ConcurrentModificationException</tt>. Thus, in the face of
638     * concurrent modification, the iterator fails quickly and cleanly, rather
639     * than risking arbitrary, non-deterministic behavior at an undetermined
640     * time in the future.
641     *
642     * @param index index of the first element to be returned from the
643     * list-iterator (by a call to <tt>next</tt>)
644     * @return a ListIterator of the elements in this list (in proper
645     * sequence), starting at the specified position in the list
646     * @throws IndexOutOfBoundsException {@inheritDoc}
647     * @see List#listIterator(int)
648     */
649     public ListIterator<E> listIterator(int index) {
650     if (index < 0 || index > size)
651     throw new IndexOutOfBoundsException("Index: "+index);
652     return new ArrayListIterator(index);
653     }
654 jsr166 1.4
655 dl 1.1 /**
656     * Returns an iterator over the elements in this list in proper sequence.
657     *
658     * @return an iterator over the elements in this list in proper sequence
659     */
660     public Iterator<E> iterator() {
661     return new ArrayListIterator(0);
662     }
663    
664     /**
665 jsr166 1.4 * A streamlined version of AbstractList.Itr
666 dl 1.1 */
667     final class ArrayListIterator implements ListIterator<E> {
668     int cursor; // index of next element to return;
669     int lastRet; // index of last element, or -1 if no such
670     int expectedModCount; // to check for CME
671    
672     ArrayListIterator(int index) {
673     cursor = index;
674     lastRet = -1;
675     expectedModCount = modCount;
676     }
677    
678     public boolean hasNext() {
679     return cursor < size;
680     }
681    
682     public boolean hasPrevious() {
683     return cursor > 0;
684     }
685    
686     public int nextIndex() {
687     return cursor;
688     }
689    
690     public int previousIndex() {
691     return cursor - 1;
692     }
693    
694     public E next() {
695     if (expectedModCount == modCount) {
696     int i = cursor;
697     if (i < size) {
698     try {
699     E e = (E)elementData[i];
700     lastRet = i;
701     cursor = i + 1;
702     return e;
703 jsr166 1.4 } catch (IndexOutOfBoundsException fallthrough) {
704 dl 1.1 }
705     }
706     }
707     // Prefer reporting CME if applicable on failures
708     if (expectedModCount == modCount)
709     throw new NoSuchElementException();
710     throw new ConcurrentModificationException();
711     }
712    
713     public E previous() {
714     if (expectedModCount == modCount) {
715     int i = cursor - 1;
716     if (i < size) {
717     try {
718     E e = (E)elementData[i];
719     lastRet = i;
720     cursor = i;
721     return e;
722 jsr166 1.4 } catch (IndexOutOfBoundsException fallthrough) {
723 dl 1.1 }
724     }
725     }
726     if (expectedModCount == modCount)
727     throw new NoSuchElementException();
728     throw new ConcurrentModificationException();
729     }
730    
731     public void remove() {
732     if (lastRet < 0)
733     throw new IllegalStateException();
734 jsr166 1.4 if (modCount != expectedModCount)
735 dl 1.1 throw new ConcurrentModificationException();
736     ArrayList.this.remove(lastRet);
737     if (lastRet < cursor)
738     cursor--;
739     lastRet = -1;
740     expectedModCount = modCount;
741     }
742    
743     public void set(E e) {
744     if (lastRet < 0)
745     throw new IllegalStateException();
746 jsr166 1.4 if (modCount != expectedModCount)
747 dl 1.1 throw new ConcurrentModificationException();
748     ArrayList.this.set(lastRet, e);
749     expectedModCount = modCount;
750     }
751    
752     public void add(E e) {
753 jsr166 1.4 if (modCount != expectedModCount)
754 dl 1.1 throw new ConcurrentModificationException();
755     ArrayList.this.add(cursor++, e);
756     lastRet = -1;
757     expectedModCount = modCount;
758     }
759     }
760    
761     }