ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/ArrayList.java
Revision: 1.1
Committed: Fri Nov 25 13:27:05 2005 UTC (18 years, 5 months ago) by dl
Branch: MAIN
Log Message:
Perfromance improvements; safer collection constructor

File Contents

# Content
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 * @param initialCapacity the initial capacity of the list
105 * @exception IllegalArgumentException if the specified initial capacity
106 * is negative
107 */
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 * iterator.
127 *
128 * @param c the collection whose elements are to be placed into this list
129 * @throws NullPointerException if the specified collection is null
130 */
131 public ArrayList(Collection<? extends E> c) {
132 Object[] a = c.toArray();
133 // If c.toArray incorrectly doesn't return Object[], copy it.
134 if (a.getClass() != Object[].class)
135 a = Arrays.copyOf(a, a.length, Object[].class);
136 elementData = a;
137 size = a.length;
138 }
139
140 /**
141 * Trims the capacity of this <tt>ArrayList</tt> instance to be the
142 * list's current size. An application can use this operation to minimize
143 * the storage of an <tt>ArrayList</tt> instance.
144 */
145 public void trimToSize() {
146 modCount++;
147 int oldCapacity = elementData.length;
148 if (size < oldCapacity) {
149 elementData = Arrays.copyOf(elementData, size);
150 }
151 }
152
153 /**
154 * Increases the capacity of this <tt>ArrayList</tt> instance, if
155 * necessary, to ensure that it can hold at least the number of elements
156 * specified by the minimum capacity argument.
157 *
158 * @param minCapacity the desired minimum capacity
159 */
160 /**
161 * Increases the capacity of this <tt>ArrayList</tt> instance, if
162 * necessary, to ensure that it can hold at least the number of elements
163 * specified by the minimum capacity argument.
164 *
165 * @param minCapacity the desired minimum capacity
166 */
167 public void ensureCapacity(int minCapacity) {
168 modCount++;
169 if (minCapacity > elementData.length)
170 growArray(minCapacity);
171 }
172
173 /**
174 * Increase the capacity of the array.
175 * @param minCapacity the desired minimum capacity
176 */
177 private void growArray(int minCapacity) {
178 int oldCapacity = elementData.length;
179 // Double size if small; else grow by 50%
180 int newCapacity = ((oldCapacity < 64)?
181 (oldCapacity * 2):
182 ((oldCapacity * 3)/2 + 1));
183 if (newCapacity < minCapacity)
184 newCapacity = minCapacity;
185 elementData = Arrays.copyOf(elementData, newCapacity);
186 }
187
188 /**
189 * Returns the number of elements in this list.
190 *
191 * @return the number of elements in this list
192 */
193 public int size() {
194 return size;
195 }
196
197 /**
198 * Returns <tt>true</tt> if this list contains no elements.
199 *
200 * @return <tt>true</tt> if this list contains no elements
201 */
202 public boolean isEmpty() {
203 return size == 0;
204 }
205
206 /**
207 * Returns <tt>true</tt> if this list contains the specified element.
208 * More formally, returns <tt>true</tt> if and only if this list contains
209 * at least one element <tt>e</tt> such that
210 * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
211 *
212 * @param o element whose presence in this list is to be tested
213 * @return <tt>true</tt> if this list contains the specified element
214 */
215 public boolean contains(Object o) {
216 return indexOf(o) >= 0;
217 }
218
219 /**
220 * Returns the index of the first occurrence of the specified element
221 * in this list, or -1 if this list does not contain the element.
222 * More formally, returns the lowest index <tt>i</tt> such that
223 * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
224 * or -1 if there is no such index.
225 */
226 public int indexOf(Object o) {
227 if (o == null) {
228 for (int i = 0; i < size; i++)
229 if (elementData[i]==null)
230 return i;
231 } else {
232 for (int i = 0; i < size; i++)
233 if (o.equals(elementData[i]))
234 return i;
235 }
236 return -1;
237 }
238
239 /**
240 * Returns the index of the last occurrence of the specified element
241 * in this list, or -1 if this list does not contain the element.
242 * More formally, returns the highest index <tt>i</tt> such that
243 * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
244 * or -1 if there is no such index.
245 */
246 public int lastIndexOf(Object o) {
247 if (o == null) {
248 for (int i = size-1; i >= 0; i--)
249 if (elementData[i]==null)
250 return i;
251 } else {
252 for (int i = size-1; i >= 0; i--)
253 if (o.equals(elementData[i]))
254 return i;
255 }
256 return -1;
257 }
258
259 /**
260 * Returns a shallow copy of this <tt>ArrayList</tt> instance. (The
261 * elements themselves are not copied.)
262 *
263 * @return a clone of this <tt>ArrayList</tt> instance
264 */
265 public Object clone() {
266 try {
267 ArrayList<E> v = (ArrayList<E>) super.clone();
268 v.elementData = Arrays.copyOf(elementData, size);
269 v.modCount = 0;
270 return v;
271 } catch (CloneNotSupportedException e) {
272 // this shouldn't happen, since we are Cloneable
273 throw new InternalError();
274 }
275 }
276
277 /**
278 * Returns an array containing all of the elements in this list
279 * in proper sequence (from first to last element).
280 *
281 * <p>The returned array will be "safe" in that no references to it are
282 * maintained by this list. (In other words, this method must allocate
283 * a new array). The caller is thus free to modify the returned array.
284 *
285 * <p>This method acts as bridge between array-based and collection-based
286 * APIs.
287 *
288 * @return an array containing all of the elements in this list in
289 * proper sequence
290 */
291 public Object[] toArray() {
292 return Arrays.copyOf(elementData, size);
293 }
294
295 /**
296 * Returns an array containing all of the elements in this list in proper
297 * sequence (from first to last element); the runtime type of the returned
298 * array is that of the specified array. If the list fits in the
299 * specified array, it is returned therein. Otherwise, a new array is
300 * allocated with the runtime type of the specified array and the size of
301 * this list.
302 *
303 * <p>If the list fits in the specified array with room to spare
304 * (i.e., the array has more elements than the list), the element in
305 * the array immediately following the end of the collection is set to
306 * <tt>null</tt>. (This is useful in determining the length of the
307 * list <i>only</i> if the caller knows that the list does not contain
308 * any null elements.)
309 *
310 * @param a the array into which the elements of the list are to
311 * be stored, if it is big enough; otherwise, a new array of the
312 * same runtime type is allocated for this purpose.
313 * @return an array containing the elements of the list
314 * @throws ArrayStoreException if the runtime type of the specified array
315 * is not a supertype of the runtime type of every element in
316 * this list
317 * @throws NullPointerException if the specified array is null
318 */
319 public <T> T[] toArray(T[] a) {
320 if (a.length < size)
321 // Make a new array of a's runtime type, but my contents:
322 return (T[]) Arrays.copyOf(elementData, size, a.getClass());
323 System.arraycopy(elementData, 0, a, 0, size);
324 if (a.length > size)
325 a[size] = null;
326 return a;
327 }
328
329 // Positional Access Operations
330
331 /**
332 * Create and return an appropriate exception for indexing errors
333 */
334 private static IndexOutOfBoundsException rangeException(int i, int s) {
335 return new IndexOutOfBoundsException("Index: " + i + ", Size: " + s);
336 }
337
338 // Positional Access Operations
339
340 /**
341 * Returns the element at the specified position in this list.
342 *
343 * @param index index of the element to return
344 * @return the element at the specified position in this list
345 * @throws IndexOutOfBoundsException {@inheritDoc}
346 */
347 public E get(int index) {
348 if (index >= size)
349 throw rangeException(index, size);
350 return (E)elementData[index];
351 }
352
353 /**
354 * Replaces the element at the specified position in this list with
355 * the specified element.
356 *
357 * @param index index of the element to replace
358 * @param element element to be stored at the specified position
359 * @return the element previously at the specified position
360 * @throws IndexOutOfBoundsException {@inheritDoc}
361 */
362 public E set(int index, E element) {
363 if (index >= size)
364 throw rangeException(index, size);
365
366 E oldValue = (E) elementData[index];
367 elementData[index] = element;
368 return oldValue;
369 }
370
371 /**
372 * Appends the specified element to the end of this list.
373 *
374 * @param e element to be appended to this list
375 * @return <tt>true</tt> (as specified by {@link Collection#add})
376 */
377 public boolean add(E e) {
378 ++modCount;
379 int s = size++;
380 if (s >= elementData.length)
381 growArray(s + 1);
382 elementData[s] = e;
383 return true;
384 }
385
386 /**
387 * Inserts the specified element at the specified position in this
388 * list. Shifts the element currently at that position (if any) and
389 * any subsequent elements to the right (adds one to their indices).
390 *
391 * @param index index at which the specified element is to be inserted
392 * @param element element to be inserted
393 * @throws IndexOutOfBoundsException {@inheritDoc}
394 */
395 public void add(int index, E element) {
396 int s = size;
397 if (index > s || index < 0)
398 throw rangeException(index, s);
399 ++modCount;
400 size = s + 1;
401 if (s >= elementData.length)
402 growArray(s + 1);
403 System.arraycopy(elementData, index, elementData, index + 1,
404 s - index);
405 elementData[index] = element;
406 }
407
408 /**
409 * Removes the element at the specified position in this list.
410 * Shifts any subsequent elements to the left (subtracts one from their
411 * indices).
412 *
413 * @param index the index of the element to be removed
414 * @return the element that was removed from the list
415 * @throws IndexOutOfBoundsException {@inheritDoc}
416 */
417 public E remove(int index) {
418 int s = size - 1;
419 if (index > s)
420 throw rangeException(index, size);
421 size = s;
422 modCount++;
423 Object oldValue = elementData[index];
424 int numMoved = s - index;
425 if (numMoved > 0)
426 System.arraycopy(elementData, index+1, elementData, index,
427 numMoved);
428 elementData[s] = null; // forget removed element
429 return (E)oldValue;
430 }
431
432 /**
433 * Removes the first occurrence of the specified element from this list,
434 * if it is present. If the list does not contain the element, it is
435 * unchanged. More formally, removes the element with the lowest index
436 * <tt>i</tt> such that
437 * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>
438 * (if such an element exists). Returns <tt>true</tt> if this list
439 * contained the specified element (or equivalently, if this list
440 * changed as a result of the call).
441 *
442 * @param o element to be removed from this list, if present
443 * @return <tt>true</tt> if this list contained the specified element
444 */
445 public boolean remove(Object o) {
446 if (o == null) {
447 for (int index = 0; index < size; index++)
448 if (elementData[index] == null) {
449 fastRemove(index);
450 return true;
451 }
452 } else {
453 for (int index = 0; index < size; index++)
454 if (o.equals(elementData[index])) {
455 fastRemove(index);
456 return true;
457 }
458 }
459 return false;
460 }
461
462 /*
463 * Private remove method that skips bounds checking and does not
464 * return the value removed.
465 */
466 private void fastRemove(int index) {
467 modCount++;
468 int numMoved = size - index - 1;
469 if (numMoved > 0)
470 System.arraycopy(elementData, index+1, elementData, index,
471 numMoved);
472 elementData[--size] = null; // Let gc do its work
473 }
474
475 /**
476 * Removes all of the elements from this list. The list will
477 * be empty after this call returns.
478 */
479 public void clear() {
480 modCount++;
481
482 // Let gc do its work
483 for (int i = 0; i < size; i++)
484 elementData[i] = null;
485
486 size = 0;
487 }
488
489 /**
490 * Appends all of the elements in the specified collection to the end of
491 * this list, in the order that they are returned by the
492 * specified collection's Iterator. The behavior of this operation is
493 * undefined if the specified collection is modified while the operation
494 * is in progress. (This implies that the behavior of this call is
495 * undefined if the specified collection is this list, and this
496 * list is nonempty.)
497 *
498 * @param c collection containing elements to be added to this list
499 * @return <tt>true</tt> if this list changed as a result of the call
500 * @throws NullPointerException if the specified collection is null
501 */
502 public boolean addAll(Collection<? extends E> c) {
503 Object[] a = c.toArray();
504 int numNew = a.length;
505 ensureCapacity(size + numNew); // Increments modCount
506 System.arraycopy(a, 0, elementData, size, numNew);
507 size += numNew;
508 return numNew != 0;
509 }
510
511 /**
512 * Inserts all of the elements in the specified collection into this
513 * list, starting at the specified position. Shifts the element
514 * currently at that position (if any) and any subsequent elements to
515 * the right (increases their indices). The new elements will appear
516 * in the list in the order that they are returned by the
517 * specified collection's iterator.
518 *
519 * @param index index at which to insert the first element from the
520 * specified collection
521 * @param c collection containing elements to be added to this list
522 * @return <tt>true</tt> if this list changed as a result of the call
523 * @throws IndexOutOfBoundsException {@inheritDoc}
524 * @throws NullPointerException if the specified collection is null
525 */
526 public boolean addAll(int index, Collection<? extends E> c) {
527 if (index > size || index < 0)
528 throw new IndexOutOfBoundsException(
529 "Index: " + index + ", Size: " + size);
530
531 Object[] a = c.toArray();
532 int numNew = a.length;
533 ensureCapacity(size + numNew); // Increments modCount
534
535 int numMoved = size - index;
536 if (numMoved > 0)
537 System.arraycopy(elementData, index, elementData, index + numNew,
538 numMoved);
539
540 System.arraycopy(a, 0, elementData, index, numNew);
541 size += numNew;
542 return numNew != 0;
543 }
544
545 /**
546 * Removes from this list all of the elements whose index is between
547 * <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive.
548 * Shifts any succeeding elements to the left (reduces their index).
549 * This call shortens the list by <tt>(toIndex - fromIndex)</tt> elements.
550 * (If <tt>toIndex==fromIndex</tt>, this operation has no effect.)
551 *
552 * @param fromIndex index of first element to be removed
553 * @param toIndex index after last element to be removed
554 * @throws IndexOutOfBoundsException if fromIndex or toIndex out of
555 * range (fromIndex &lt; 0 || fromIndex &gt;= size() || toIndex
556 * &gt; size() || toIndex &lt; fromIndex)
557 */
558 protected void removeRange(int fromIndex, int toIndex) {
559 modCount++;
560 int numMoved = size - toIndex;
561 System.arraycopy(elementData, toIndex, elementData, fromIndex,
562 numMoved);
563
564 // Let gc do its work
565 int newSize = size - (toIndex-fromIndex);
566 while (size != newSize)
567 elementData[--size] = null;
568 }
569
570 /**
571 * Save the state of the <tt>ArrayList</tt> instance to a stream (that
572 * is, serialize it).
573 *
574 * @serialData The length of the array backing the <tt>ArrayList</tt>
575 * instance is emitted (int), followed by all of its elements
576 * (each an <tt>Object</tt>) in the proper order.
577 */
578 private void writeObject(java.io.ObjectOutputStream s)
579 throws java.io.IOException{
580 // Write out element count, and any hidden stuff
581 int expectedModCount = modCount;
582 s.defaultWriteObject();
583
584 // Write out array length
585 s.writeInt(elementData.length);
586
587 // Write out all elements in the proper order.
588 for (int i=0; i<size; i++)
589 s.writeObject(elementData[i]);
590
591 if (modCount != expectedModCount) {
592 throw new ConcurrentModificationException();
593 }
594
595 }
596
597 /**
598 * Reconstitute the <tt>ArrayList</tt> instance from a stream (that is,
599 * deserialize it).
600 */
601 private void readObject(java.io.ObjectInputStream s)
602 throws java.io.IOException, ClassNotFoundException {
603 // Read in size, and any hidden stuff
604 s.defaultReadObject();
605
606 // Read in array length and allocate array
607 int arrayLength = s.readInt();
608 Object[] a = elementData = new Object[arrayLength];
609
610 // Read in all elements in the proper order.
611 for (int i=0; i<size; i++)
612 a[i] = s.readObject();
613 }
614
615
616 /**
617 * Returns a list-iterator of the elements in this list (in proper
618 * sequence), starting at the specified position in the list.
619 * Obeys the general contract of <tt>List.listIterator(int)</tt>.<p>
620 *
621 * The list-iterator is <i>fail-fast</i>: if the list is structurally
622 * modified at any time after the Iterator is created, in any way except
623 * through the list-iterator's own <tt>remove</tt> or <tt>add</tt>
624 * methods, the list-iterator will throw a
625 * <tt>ConcurrentModificationException</tt>. Thus, in the face of
626 * concurrent modification, the iterator fails quickly and cleanly, rather
627 * than risking arbitrary, non-deterministic behavior at an undetermined
628 * time in the future.
629 *
630 * @param index index of the first element to be returned from the
631 * list-iterator (by a call to <tt>next</tt>)
632 * @return a ListIterator of the elements in this list (in proper
633 * sequence), starting at the specified position in the list
634 * @throws IndexOutOfBoundsException {@inheritDoc}
635 * @see List#listIterator(int)
636 */
637 public ListIterator<E> listIterator(int index) {
638 if (index < 0 || index > size)
639 throw new IndexOutOfBoundsException("Index: "+index);
640 return new ArrayListIterator(index);
641 }
642
643 /**
644 * Returns an iterator over the elements in this list in proper sequence.
645 *
646 * @return an iterator over the elements in this list in proper sequence
647 */
648 public Iterator<E> iterator() {
649 return new ArrayListIterator(0);
650 }
651
652 /**
653 * A streamlined version of AbstractList.Itr
654 */
655 final class ArrayListIterator implements ListIterator<E> {
656 int cursor; // index of next element to return;
657 int lastRet; // index of last element, or -1 if no such
658 int expectedModCount; // to check for CME
659
660 ArrayListIterator(int index) {
661 cursor = index;
662 lastRet = -1;
663 expectedModCount = modCount;
664 }
665
666 public boolean hasNext() {
667 return cursor < size;
668 }
669
670 public boolean hasPrevious() {
671 return cursor > 0;
672 }
673
674 public int nextIndex() {
675 return cursor;
676 }
677
678 public int previousIndex() {
679 return cursor - 1;
680 }
681
682 public E next() {
683 if (expectedModCount == modCount) {
684 int i = cursor;
685 if (i < size) {
686 try {
687 E e = (E)elementData[i];
688 lastRet = i;
689 cursor = i + 1;
690 return e;
691 } catch (IndexOutOfBoundsException fallthrough) {
692 }
693 }
694 }
695 // Prefer reporting CME if applicable on failures
696 if (expectedModCount == modCount)
697 throw new NoSuchElementException();
698 throw new ConcurrentModificationException();
699 }
700
701 public E previous() {
702 if (expectedModCount == modCount) {
703 int i = cursor - 1;
704 if (i < size) {
705 try {
706 E e = (E)elementData[i];
707 lastRet = i;
708 cursor = i;
709 return e;
710 } catch (IndexOutOfBoundsException fallthrough) {
711 }
712 }
713 }
714 if (expectedModCount == modCount)
715 throw new NoSuchElementException();
716 throw new ConcurrentModificationException();
717 }
718
719 public void remove() {
720 if (lastRet < 0)
721 throw new IllegalStateException();
722 if (modCount != expectedModCount)
723 throw new ConcurrentModificationException();
724 ArrayList.this.remove(lastRet);
725 if (lastRet < cursor)
726 cursor--;
727 lastRet = -1;
728 expectedModCount = modCount;
729 }
730
731 public void set(E e) {
732 if (lastRet < 0)
733 throw new IllegalStateException();
734 if (modCount != expectedModCount)
735 throw new ConcurrentModificationException();
736 ArrayList.this.set(lastRet, e);
737 expectedModCount = modCount;
738 }
739
740 public void add(E e) {
741 if (modCount != expectedModCount)
742 throw new ConcurrentModificationException();
743 ArrayList.this.add(cursor++, e);
744 lastRet = -1;
745 expectedModCount = modCount;
746 }
747 }
748
749 }