91 |
|
* should be used only to detect bugs.</i> |
92 |
|
* |
93 |
|
* <p>This class is a member of the |
94 |
< |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
94 |
> |
* <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework"> |
95 |
|
* Java Collections Framework</a>. |
96 |
|
* |
97 |
|
* @param <E> the type of elements in this list |
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; |
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 |
|
/** |
575 |
|
*/ |
576 |
|
public void clear() { |
577 |
|
modCount++; |
578 |
< |
Arrays.fill(elementData, 0, size, null); |
579 |
< |
size = 0; |
578 |
> |
final Object[] es = elementData; |
579 |
> |
for (int to = size, i = size = 0; i < to; i++) |
580 |
> |
es[i] = null; |
581 |
|
} |
582 |
|
|
583 |
|
/** |
667 |
|
outOfBoundsMsg(fromIndex, toIndex)); |
668 |
|
} |
669 |
|
modCount++; |
670 |
< |
final Object[] es = elementData; |
673 |
< |
final int oldSize = size; |
674 |
< |
System.arraycopy(es, toIndex, es, fromIndex, oldSize - toIndex); |
675 |
< |
Arrays.fill(es, size -= (toIndex - fromIndex), oldSize, null); |
670 |
> |
shiftTailOverGap(elementData, fromIndex, toIndex); |
671 |
|
// checkInvariants(); |
672 |
|
} |
673 |
|
|
674 |
+ |
/** Erases the gap from lo to hi, by sliding down following elements. */ |
675 |
+ |
private void shiftTailOverGap(Object[] es, int lo, int hi) { |
676 |
+ |
System.arraycopy(es, hi, es, lo, size - hi); |
677 |
+ |
for (int to = size, i = (size -= hi - lo); i < to; i++) |
678 |
+ |
es[i] = null; |
679 |
+ |
} |
680 |
+ |
|
681 |
|
/** |
682 |
|
* A version of rangeCheck used by add and addAll. |
683 |
|
*/ |
745 |
|
final int from, final int end) { |
746 |
|
Objects.requireNonNull(c); |
747 |
|
final Object[] es = elementData; |
746 |
– |
final boolean modified; |
748 |
|
int r; |
749 |
|
// Optimize for initial run of survivors |
750 |
< |
for (r = from; r < end && c.contains(es[r]) == complement; r++) |
751 |
< |
; |
752 |
< |
if (modified = (r < end)) { |
753 |
< |
int w = r++; |
754 |
< |
try { |
755 |
< |
for (Object e; r < end; r++) |
756 |
< |
if (c.contains(e = es[r]) == complement) |
757 |
< |
es[w++] = e; |
758 |
< |
} catch (Throwable ex) { |
759 |
< |
// Preserve behavioral compatibility with AbstractCollection, |
760 |
< |
// even if c.contains() throws. |
761 |
< |
System.arraycopy(es, r, es, w, end - r); |
762 |
< |
w += end - r; |
763 |
< |
throw ex; |
764 |
< |
} finally { |
765 |
< |
final int oldSize = size, deleted = end - w; |
766 |
< |
modCount += deleted; |
767 |
< |
System.arraycopy(es, end, es, w, oldSize - end); |
768 |
< |
Arrays.fill(es, size -= deleted, oldSize, null); |
769 |
< |
} |
750 |
> |
for (r = from;; r++) { |
751 |
> |
if (r == end) |
752 |
> |
return false; |
753 |
> |
if (c.contains(es[r]) != complement) |
754 |
> |
break; |
755 |
> |
} |
756 |
> |
int w = r++; |
757 |
> |
try { |
758 |
> |
for (Object e; r < end; r++) |
759 |
> |
if (c.contains(e = es[r]) == complement) |
760 |
> |
es[w++] = e; |
761 |
> |
} catch (Throwable ex) { |
762 |
> |
// Preserve behavioral compatibility with AbstractCollection, |
763 |
> |
// even if c.contains() throws. |
764 |
> |
System.arraycopy(es, r, es, w, end - r); |
765 |
> |
w += end - r; |
766 |
> |
throw ex; |
767 |
> |
} finally { |
768 |
> |
modCount += end - w; |
769 |
> |
shiftTailOverGap(es, w, end); |
770 |
|
} |
771 |
|
// checkInvariants(); |
772 |
< |
return modified; |
772 |
> |
return true; |
773 |
|
} |
774 |
|
|
775 |
|
/** |
788 |
|
int expectedModCount = modCount; |
789 |
|
s.defaultWriteObject(); |
790 |
|
|
791 |
< |
// Write out size as capacity for behavioural compatibility with clone() |
791 |
> |
// Write out size as capacity for behavioral compatibility with clone() |
792 |
|
s.writeInt(size); |
793 |
|
|
794 |
|
// Write out all elements in the proper order. |
1369 |
|
} |
1370 |
|
} |
1371 |
|
|
1372 |
+ |
/** |
1373 |
+ |
* @throws NullPointerException {@inheritDoc} |
1374 |
+ |
*/ |
1375 |
|
@Override |
1376 |
|
public void forEach(Consumer<? super E> action) { |
1377 |
|
Objects.requireNonNull(action); |
1441 |
|
private int fence; // -1 until used; then one past last index |
1442 |
|
private int expectedModCount; // initialized when fence set |
1443 |
|
|
1444 |
< |
/** Create new spliterator covering the given range */ |
1444 |
> |
/** Creates new spliterator covering the given range. */ |
1445 |
|
ArrayListSpliterator(int origin, int fence, int expectedModCount) { |
1446 |
|
this.index = origin; |
1447 |
|
this.fence = fence; |
1523 |
|
return (bits[i >> 6] & (1L << i)) == 0; |
1524 |
|
} |
1525 |
|
|
1526 |
+ |
/** |
1527 |
+ |
* @throws NullPointerException {@inheritDoc} |
1528 |
+ |
*/ |
1529 |
|
@Override |
1530 |
|
public boolean removeIf(Predicate<? super E> filter) { |
1531 |
|
return removeIf(filter, 0, size); |
1560 |
|
for (i = beg; i < end; i++) |
1561 |
|
if (isClear(deathRow, i - beg)) |
1562 |
|
es[w++] = es[i]; |
1563 |
< |
final int oldSize = size; |
1557 |
< |
System.arraycopy(es, end, es, w, oldSize - end); |
1558 |
< |
Arrays.fill(es, size -= (end - w), oldSize, null); |
1563 |
> |
shiftTailOverGap(es, w, end); |
1564 |
|
// checkInvariants(); |
1565 |
|
return true; |
1566 |
|
} else { |