ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/ArrayList.java
Revision: 1.2
Committed: Fri Nov 25 13:34:29 2005 UTC (18 years, 5 months ago) by dl
Branch: MAIN
Changes since 1.1: +29 -9 lines
Log Message:
Merge with other changes

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