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.50 by jsr166, Sat May 6 06:49:45 2017 UTC vs.
Revision 1.51 by jsr166, Sun May 14 21:44:25 2017 UTC

# Line 515 | Line 515 | public class ArrayList<E> extends Abstra
515       */
516      public E remove(int index) {
517          Objects.checkIndex(index, size);
518 +        final Object[] es = elementData;
519  
520 <        modCount++;
521 <        E oldValue = elementData(index);
521 <
522 <        int numMoved = size - index - 1;
523 <        if (numMoved > 0)
524 <            System.arraycopy(elementData, index+1, elementData, index,
525 <                             numMoved);
526 <        elementData[--size] = null; // clear to let GC do its work
520 >        @SuppressWarnings("unchecked") E oldValue = (E) es[index];
521 >        fastRemove(es, index);
522  
523          // checkInvariants();
524          return oldValue;
# Line 543 | Line 538 | public class ArrayList<E> extends Abstra
538       * @return {@code true} if this list contained the specified element
539       */
540      public boolean remove(Object o) {
541 <        if (o == null) {
542 <            for (int index = 0; index < size; index++)
543 <                if (elementData[index] == null) {
544 <                    fastRemove(index);
545 <                    return true;
546 <                }
547 <        } else {
548 <            for (int index = 0; index < size; index++)
549 <                if (o.equals(elementData[index])) {
550 <                    fastRemove(index);
551 <                    return true;
552 <                }
541 >        final Object[] es = elementData;
542 >        final int size = this.size;
543 >        int i = 0;
544 >        found: {
545 >            if (o == null) {
546 >                for (; i < size; i++)
547 >                    if (es[i] == null)
548 >                        break found;
549 >            } else {
550 >                for (; i < size; i++)
551 >                    if (o.equals(es[i]))
552 >                        break found;
553 >            }
554 >            return false;
555          }
556 <        return false;
556 >        fastRemove(es, i);
557 >        return true;
558      }
559  
560      /**
561       * Private remove method that skips bounds checking and does not
562       * return the value removed.
563       */
564 <    private void fastRemove(int index) {
564 >    private void fastRemove(Object[] es, int i) {
565          modCount++;
566 <        int numMoved = size - index - 1;
567 <        if (numMoved > 0)
568 <            System.arraycopy(elementData, index+1, elementData, index,
569 <                             numMoved);
572 <        elementData[--size] = null; // clear to let GC do its work
566 >        final int newSize;
567 >        if ((newSize = size - 1) > i)
568 >            System.arraycopy(es, i + 1, es, i, newSize - i);
569 >        es[size = newSize] = null;
570      }
571  
572      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines