ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/ArrayList.java
(Generate patch)

Comparing jsr166/src/main/java/util/ArrayList.java (file contents):
Revision 1.4 by jsr166, Sat Nov 26 03:12:10 2005 UTC vs.
Revision 1.10 by jsr166, Mon Nov 28 02:35:46 2005 UTC

# Line 101 | Line 101 | public class ArrayList<E> extends Abstra
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
104 >     * @param initialCapacity the initial capacity of the list
105 >     * @throws IllegalArgumentException if the specified initial capacity
106 >     *         is negative
107       */
108      public ArrayList(int initialCapacity) {
109          super();
# Line 175 | Line 175 | public class ArrayList<E> extends Abstra
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
178 >     * @param minCapacity the desired minimum capacity
179       */
180      public void ensureCapacity(int minCapacity) {
181          modCount++;
# Line 184 | Line 184 | public class ArrayList<E> extends Abstra
184      }
185  
186      /**
187 <     * Increase the capacity of the array.
188 <     * @param   minCapacity   the desired minimum capacity
187 >     * Increases the capacity of the array.
188 >     *
189 >     * @param minCapacity the desired minimum capacity
190       */
191      private void growArray(int minCapacity) {
192 +        if (minCapacity < 0)
193 +            throw new OutOfMemoryError(); // int overflow
194          int oldCapacity = elementData.length;
195          // Double size if small; else grow by 50%
196          int newCapacity = ((oldCapacity < 64)?
197 <                           (oldCapacity * 2):
198 <                           ((oldCapacity * 3)/2 + 1));
197 >                           ((oldCapacity + 1) * 2):
198 >                           ((oldCapacity * 3) / 2));
199          if (newCapacity < minCapacity)
200              newCapacity = minCapacity;
201          elementData = Arrays.copyOf(elementData, newCapacity);
# Line 342 | Line 345 | public class ArrayList<E> extends Abstra
345      // Positional Access Operations
346  
347      /**
348 <     * Create and return an appropriate exception for indexing errors
348 >     * Returns error message string for IndexOutOfBoundsExceptions
349       */
350 <    private static IndexOutOfBoundsException rangeException(int i, int s) {
351 <        return new IndexOutOfBoundsException("Index: " + i + ", Size: " + s);
350 >    private String ioobe(int index) {
351 >        return "Index: " + index + ", Size: " + size;
352      }
353  
351    // Positional Access Operations
352
354      /**
355       * Returns the element at the specified position in this list.
356       *
# Line 359 | Line 360 | public class ArrayList<E> extends Abstra
360       */
361      public E get(int index) {
362          if (index >= size)
363 <            throw rangeException(index, size);
363 >            throw new IndexOutOfBoundsException(ioobe(index));
364          return (E)elementData[index];
365      }
366  
# Line 374 | Line 375 | public class ArrayList<E> extends Abstra
375       */
376      public E set(int index, E element) {
377          if (index >= size)
378 <             throw rangeException(index, size);
378 >            throw new IndexOutOfBoundsException(ioobe(index));
379  
380          E oldValue = (E) elementData[index];
381          elementData[index] = element;
# Line 388 | Line 389 | public class ArrayList<E> extends Abstra
389       * @return <tt>true</tt> (as specified by {@link Collection#add})
390       */
391      public boolean add(E e) {
392 <        ++modCount;
393 <        int s = size++;
392 >        modCount++;
393 >        int s = size;
394          if (s >= elementData.length)
395              growArray(s + 1);
396          elementData[s] = e;
397 +        size = s + 1;
398          return true;
399      }
400  
# Line 408 | Line 410 | public class ArrayList<E> extends Abstra
410      public void add(int index, E element) {
411          int s = size;
412          if (index > s || index < 0)
413 <            throw rangeException(index, s);
414 <        ++modCount;
413 <        size = s + 1;
413 >            throw new IndexOutOfBoundsException(ioobe(index));
414 >        modCount++;
415          if (s >= elementData.length)
416              growArray(s + 1);
417 <        System.arraycopy(elementData, index, elementData, index + 1,
418 <                         s - index);
417 >        System.arraycopy(elementData, index,
418 >                         elementData, index + 1, s - index);
419          elementData[index] = element;
420 +        size = s + 1;
421      }
422  
423      /**
# Line 430 | Line 432 | public class ArrayList<E> extends Abstra
432      public E remove(int index) {
433          int s = size - 1;
434          if (index > s)
435 <            throw rangeException(index, size);
434 <        size = s;
435 >            throw new IndexOutOfBoundsException(ioobe(index));
436          modCount++;
437 <        Object oldValue = elementData[index];
437 >        E oldValue = (E)elementData[index];
438          int numMoved = s - index;
439          if (numMoved > 0)
440 <            System.arraycopy(elementData, index+1, elementData, index,
441 <                             numMoved);
442 <        elementData[s] = null; // forget removed element
443 <        return (E)oldValue;
440 >            System.arraycopy(elementData, index + 1,
441 >                             elementData, index, numMoved);
442 >        elementData[s] = null;
443 >        size = s;
444 >        return oldValue;
445      }
446  
447      /**
# Line 538 | Line 540 | public class ArrayList<E> extends Abstra
540       */
541      public boolean addAll(int index, Collection<? extends E> c) {
542          if (index > size || index < 0)
543 <            throw new IndexOutOfBoundsException(
542 <                "Index: " + index + ", Size: " + size);
543 >            throw new IndexOutOfBoundsException(ioobe(index));
544  
545          Object[] a = c.toArray();
546          int numNew = a.length;
# Line 649 | Line 650 | public class ArrayList<E> extends Abstra
650       */
651      public ListIterator<E> listIterator(int index) {
652          if (index < 0 || index > size)
653 <            throw new IndexOutOfBoundsException("Index: "+index);
653 >            throw new IndexOutOfBoundsException(ioobe(index));
654          return new ArrayListIterator(index);
655      }
656  
657      /**
658 +     * {@inheritDoc}
659 +     */
660 +    public ListIterator<E> listIterator() {
661 +        return new ArrayListIterator(0);
662 +    }
663 +
664 +    /**
665       * Returns an iterator over the elements in this list in proper sequence.
666       *
667       * @return an iterator over the elements in this list in proper sequence
# Line 663 | Line 671 | public class ArrayList<E> extends Abstra
671      }
672  
673      /**
674 <     * A streamlined version of AbstractList.Itr
674 >     * A streamlined version of AbstractList.ListItr
675       */
676      final class ArrayListIterator implements ListIterator<E> {
677          int cursor;           // index of next element to return;
# Line 693 | Line 701 | public class ArrayList<E> extends Abstra
701          }
702  
703          public E next() {
704 <            if (expectedModCount == modCount) {
704 >            try {
705                  int i = cursor;
706 <                if (i < size) {
707 <                    try {
708 <                        E e = (E)elementData[i];
709 <                        lastRet = i;
710 <                        cursor = i + 1;
703 <                        return e;
704 <                    } catch (IndexOutOfBoundsException fallthrough) {
705 <                    }
706 <                }
707 <            }
708 <            // Prefer reporting CME if applicable on failures
709 <            if (expectedModCount == modCount)
706 >                E next = get(i);
707 >                lastRet = i;
708 >                cursor = i + 1;
709 >                return next;
710 >            } catch (IndexOutOfBoundsException ex) {
711                  throw new NoSuchElementException();
712 <            throw new ConcurrentModificationException();
712 >            } finally {
713 >                if (expectedModCount != modCount)
714 >                    throw new ConcurrentModificationException();
715 >            }
716          }
713
717          public E previous() {
718 <            if (expectedModCount == modCount) {
718 >            try {
719                  int i = cursor - 1;
720 <                if (i < size) {
721 <                    try {
722 <                        E e = (E)elementData[i];
723 <                        lastRet = i;
724 <                        cursor = i;
722 <                        return e;
723 <                    } catch (IndexOutOfBoundsException fallthrough) {
724 <                    }
725 <                }
726 <            }
727 <            if (expectedModCount == modCount)
720 >                E next = get(i);
721 >                lastRet = i;
722 >                cursor = i;
723 >                return next;
724 >            } catch (IndexOutOfBoundsException ex) {
725                  throw new NoSuchElementException();
726 <            throw new ConcurrentModificationException();
726 >            } finally {
727 >                if (modCount != expectedModCount)
728 >                    throw new ConcurrentModificationException();
729 >            }
730          }
731  
732          public void remove() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines