--- jsr166/src/main/java/util/ArrayList.java 2005/11/27 14:54:23 1.9 +++ jsr166/src/main/java/util/ArrayList.java 2006/02/07 20:54:24 1.16 @@ -1,12 +1,11 @@ /* * %W% %E% * - * Copyright 2005 Sun Microsystems, Inc. All rights reserved. + * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.util; -import java.util.*; // for javadoc (till 6280605 is fixed) /** * Resizable-array implementation of the List interface. Implements @@ -189,13 +188,15 @@ public class ArrayList extends Abstra * @param minCapacity the desired minimum capacity */ private void growArray(int minCapacity) { - if (minCapacity < 0) - throw new OutOfMemoryError(); // int overflow + if (minCapacity < 0) // overflow + throw new OutOfMemoryError(); int oldCapacity = elementData.length; // Double size if small; else grow by 50% int newCapacity = ((oldCapacity < 64)? ((oldCapacity + 1) * 2): - ((oldCapacity * 3) / 2)); + ((oldCapacity / 2) * 3)); + if (newCapacity < 0) // overflow + newCapacity = Integer.MAX_VALUE; if (newCapacity < minCapacity) newCapacity = minCapacity; elementData = Arrays.copyOf(elementData, newCapacity); @@ -414,7 +415,7 @@ public class ArrayList extends Abstra modCount++; if (s >= elementData.length) growArray(s + 1); - System.arraycopy(elementData, index, + System.arraycopy(elementData, index, elementData, index + 1, s - index); elementData[index] = element; size = s + 1; @@ -437,7 +438,7 @@ public class ArrayList extends Abstra E oldValue = (E)elementData[index]; int numMoved = s - index; if (numMoved > 0) - System.arraycopy(elementData, index + 1, + System.arraycopy(elementData, index + 1, elementData, index, numMoved); elementData[s] = null; size = s; @@ -602,7 +603,7 @@ public class ArrayList extends Abstra for (int i=0; i extends Abstra } public boolean hasNext() { - return cursor < size; + return cursor != size; } public boolean hasPrevious() { - return cursor > 0; + return cursor != 0; } public int nextIndex() { @@ -714,17 +715,18 @@ public class ArrayList extends Abstra throw new ConcurrentModificationException(); } } + public E previous() { try { int i = cursor - 1; - E next = get(i); + E prev = get(i); lastRet = i; cursor = i; - return next; + return prev; } catch (IndexOutOfBoundsException ex) { throw new NoSuchElementException(); } finally { - if (modCount != expectedModCount) + if (expectedModCount != modCount) throw new ConcurrentModificationException(); } } @@ -732,7 +734,7 @@ public class ArrayList extends Abstra public void remove() { if (lastRet < 0) throw new IllegalStateException(); - if (modCount != expectedModCount) + if (expectedModCount != modCount) throw new ConcurrentModificationException(); ArrayList.this.remove(lastRet); if (lastRet < cursor) @@ -744,19 +746,22 @@ public class ArrayList extends Abstra public void set(E e) { if (lastRet < 0) throw new IllegalStateException(); - if (modCount != expectedModCount) + if (expectedModCount != modCount) throw new ConcurrentModificationException(); ArrayList.this.set(lastRet, e); expectedModCount = modCount; } public void add(E e) { - if (modCount != expectedModCount) + if (expectedModCount != modCount) throw new ConcurrentModificationException(); - ArrayList.this.add(cursor++, e); - lastRet = -1; - expectedModCount = modCount; + try { + ArrayList.this.add(cursor++, e); + lastRet = -1; + expectedModCount = modCount; + } catch (IndexOutOfBoundsException ex) { + throw new ConcurrentModificationException(); + } } } - }