--- jsr166/src/main/java/util/ArrayList.java 2005/11/26 03:03:49 1.3 +++ jsr166/src/main/java/util/ArrayList.java 2005/11/26 20:39:51 1.8 @@ -101,9 +101,9 @@ public class ArrayList extends Abstra /** * Constructs an empty list with the specified initial capacity. * - * @param initialCapacity the initial capacity of the list - * @exception IllegalArgumentException if the specified initial capacity - * is negative + * @param initialCapacity the initial capacity of the list + * @throws IllegalArgumentException if the specified initial capacity + * is negative */ public ArrayList(int initialCapacity) { super(); @@ -146,7 +146,7 @@ public class ArrayList extends Abstra } initFromConcurrentlyMutating(c); } - + private void initFromConcurrentlyMutating(Collection c) { elementData = c.toArray(); size = elementData.length; @@ -154,9 +154,9 @@ public class ArrayList extends Abstra if (elementData.getClass() != Object[].class) elementData = Arrays.copyOf(elementData, size, Object[].class); } - + private final static Object UNALLOCATED = new Object(); - + /** * Trims the capacity of this ArrayList instance to be the * list's current size. An application can use this operation to minimize @@ -175,7 +175,7 @@ public class ArrayList extends Abstra * necessary, to ensure that it can hold at least the number of elements * specified by the minimum capacity argument. * - * @param minCapacity the desired minimum capacity + * @param minCapacity the desired minimum capacity */ public void ensureCapacity(int minCapacity) { modCount++; @@ -184,15 +184,17 @@ public class ArrayList extends Abstra } /** - * Increase the capacity of the array. - * @param minCapacity the desired minimum capacity + * Increases the capacity of the array. + * + * @param minCapacity the desired minimum capacity */ private void growArray(int minCapacity) { + if (minCapacity < 0) throw new OutOfMemoryError(); // int overflow int oldCapacity = elementData.length; // Double size if small; else grow by 50% - int newCapacity = ((oldCapacity < 64)? + int newCapacity = ((oldCapacity < 64)? (oldCapacity * 2): - ((oldCapacity * 3)/2 + 1)); + ((oldCapacity * 3)/2)); if (newCapacity < minCapacity) newCapacity = minCapacity; elementData = Arrays.copyOf(elementData, newCapacity); @@ -341,15 +343,13 @@ public class ArrayList extends Abstra // Positional Access Operations - /** - * Create and return an appropriate exception for indexing errors + /** + * Throws an appropriate exception for indexing errors. */ - private static IndexOutOfBoundsException rangeException(int i, int s) { - return new IndexOutOfBoundsException("Index: " + i + ", Size: " + s); + private static void rangeException(int i, int s) { + throw new IndexOutOfBoundsException("Index: " + i + ", Size: " + s); } - // Positional Access Operations - /** * Returns the element at the specified position in this list. * @@ -359,7 +359,7 @@ public class ArrayList extends Abstra */ public E get(int index) { if (index >= size) - throw rangeException(index, size); + rangeException(index, size); return (E)elementData[index]; } @@ -374,7 +374,7 @@ public class ArrayList extends Abstra */ public E set(int index, E element) { if (index >= size) - throw rangeException(index, size); + rangeException(index, size); E oldValue = (E) elementData[index]; elementData[index] = element; @@ -388,11 +388,12 @@ public class ArrayList extends Abstra * @return true (as specified by {@link Collection#add}) */ public boolean add(E e) { - ++modCount; - int s = size++; + modCount++; + int s = size; if (s >= elementData.length) growArray(s + 1); elementData[s] = e; + size = s + 1; return true; } @@ -408,14 +409,14 @@ public class ArrayList extends Abstra public void add(int index, E element) { int s = size; if (index > s || index < 0) - throw rangeException(index, s); - ++modCount; - size = s + 1; + rangeException(index, s); + modCount++; if (s >= elementData.length) growArray(s + 1); - System.arraycopy(elementData, index, elementData, index + 1, - s - index); + System.arraycopy(elementData, index, + elementData, index + 1, s - index); elementData[index] = element; + size = s + 1; } /** @@ -430,16 +431,16 @@ public class ArrayList extends Abstra public E remove(int index) { int s = size - 1; if (index > s) - throw rangeException(index, size); - size = s; + rangeException(index, size); modCount++; - Object oldValue = elementData[index]; + E oldValue = (E)elementData[index]; int numMoved = s - index; if (numMoved > 0) - System.arraycopy(elementData, index+1, elementData, index, - numMoved); - elementData[s] = null; // forget removed element - return (E)oldValue; + System.arraycopy(elementData, index + 1, + elementData, index, numMoved); + elementData[s] = null; + size = s; + return oldValue; } /** @@ -652,7 +653,7 @@ public class ArrayList extends Abstra throw new IndexOutOfBoundsException("Index: "+index); return new ArrayListIterator(index); } - + /** * Returns an iterator over the elements in this list in proper sequence. * @@ -663,7 +664,7 @@ public class ArrayList extends Abstra } /** - * A streamlined version of AbstractList.Itr + * A streamlined version of AbstractList.Itr */ final class ArrayListIterator implements ListIterator { int cursor; // index of next element to return; @@ -701,7 +702,7 @@ public class ArrayList extends Abstra lastRet = i; cursor = i + 1; return e; - } catch (IndexOutOfBoundsException fallthrough) { + } catch (IndexOutOfBoundsException fallthrough) { } } } @@ -720,7 +721,7 @@ public class ArrayList extends Abstra lastRet = i; cursor = i; return e; - } catch (IndexOutOfBoundsException fallthrough) { + } catch (IndexOutOfBoundsException fallthrough) { } } } @@ -732,7 +733,7 @@ public class ArrayList extends Abstra public void remove() { if (lastRet < 0) throw new IllegalStateException(); - if (modCount != expectedModCount) + if (modCount != expectedModCount) throw new ConcurrentModificationException(); ArrayList.this.remove(lastRet); if (lastRet < cursor) @@ -744,14 +745,14 @@ public class ArrayList extends Abstra public void set(E e) { if (lastRet < 0) throw new IllegalStateException(); - if (modCount != expectedModCount) + if (modCount != expectedModCount) throw new ConcurrentModificationException(); ArrayList.this.set(lastRet, e); expectedModCount = modCount; } public void add(E e) { - if (modCount != expectedModCount) + if (modCount != expectedModCount) throw new ConcurrentModificationException(); ArrayList.this.add(cursor++, e); lastRet = -1;