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.6 by jsr166, Sat Nov 26 04:35:16 2005 UTC vs.
Revision 1.7 by dl, Sat Nov 26 17:35:19 2005 UTC

# Line 189 | Line 189 | public class ArrayList<E> extends Abstra
189       * @param minCapacity the desired minimum capacity
190       */
191      private void growArray(int minCapacity) {
192 +        if (minCapacity < 0) throw new OutOfMemoryError(); // int overflow
193          int oldCapacity = elementData.length;
194          // Double size if small; else grow by 50%
195          int newCapacity = ((oldCapacity < 64)?
196                             (oldCapacity * 2):
197 <                           ((oldCapacity * 3)/2 + 1));
197 >                           ((oldCapacity * 3)/2));
198          if (newCapacity < minCapacity)
199              newCapacity = minCapacity;
200          elementData = Arrays.copyOf(elementData, newCapacity);
# Line 343 | Line 344 | public class ArrayList<E> extends Abstra
344      // Positional Access Operations
345  
346      /**
347 <     * Creates and returns an appropriate exception for indexing errors.
347 >     * Throws an appropriate exception for indexing errors.
348       */
349 <    private static IndexOutOfBoundsException rangeException(int i, int s) {
350 <        return new IndexOutOfBoundsException("Index: " + i + ", Size: " + s);
349 >    private static void rangeException(int i, int s) {
350 >        throw new IndexOutOfBoundsException("Index: " + i + ", Size: " + s);
351      }
352  
353      /**
# Line 358 | Line 359 | public class ArrayList<E> extends Abstra
359       */
360      public E get(int index) {
361          if (index >= size)
362 <            throw rangeException(index, size);
362 >            rangeException(index, size);
363          return (E)elementData[index];
364      }
365  
# Line 373 | Line 374 | public class ArrayList<E> extends Abstra
374       */
375      public E set(int index, E element) {
376          if (index >= size)
377 <             throw rangeException(index, size);
377 >            rangeException(index, size);
378  
379          E oldValue = (E) elementData[index];
380          elementData[index] = element;
# Line 388 | Line 389 | public class ArrayList<E> extends Abstra
389       */
390      public boolean add(E e) {
391          ++modCount;
392 <        int s = size++;
392 >        int s = size;
393          if (s >= elementData.length)
394              growArray(s + 1);
395          elementData[s] = e;
396 +        size = s + 1;
397          return true;
398      }
399  
# Line 407 | Line 409 | public class ArrayList<E> extends Abstra
409      public void add(int index, E element) {
410          int s = size;
411          if (index > s || index < 0)
412 <            throw rangeException(index, s);
412 >            rangeException(index, s);
413          ++modCount;
412        size = s + 1;
414          if (s >= elementData.length)
415              growArray(s + 1);
416 <        System.arraycopy(elementData, index, elementData, index + 1,
417 <                         s - index);
416 >        System.arraycopy(elementData, index,
417 >                         elementData, index + 1, s - index);
418          elementData[index] = element;
419 +        size = s + 1;
420      }
421  
422      /**
# Line 428 | Line 430 | public class ArrayList<E> extends Abstra
430       */
431      public E remove(int index) {
432          int s = size - 1;
433 <        if (index > s)
434 <            throw rangeException(index, size);
433 <        size = s;
433 >        if (index < 0 || index > s)
434 >            rangeException(index, size);
435          modCount++;
436 <        Object oldValue = elementData[index];
436 >        E oldValue = (E)elementData[index];
437          int numMoved = s - index;
438          if (numMoved > 0)
439 <            System.arraycopy(elementData, index+1, elementData, index,
440 <                             numMoved);
439 >            System.arraycopy(elementData, index + 1,
440 >                             elementData, index, numMoved);
441          elementData[s] = null; // forget removed element
442 <        return (E)oldValue;
442 >        size = s;
443 >        return oldValue;
444      }
445  
446      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines