578 |
|
*/ |
579 |
|
public void clear() { |
580 |
|
modCount++; |
581 |
< |
Arrays.fill(elementData, 0, size, null); |
582 |
< |
size = 0; |
581 |
> |
final Object[] es = elementData; |
582 |
> |
for (int to = size, i = size = 0; i < to; i++) |
583 |
> |
es[i] = null; |
584 |
|
} |
585 |
|
|
586 |
|
/** |
670 |
|
outOfBoundsMsg(fromIndex, toIndex)); |
671 |
|
} |
672 |
|
modCount++; |
673 |
< |
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); |
673 |
> |
shiftTailOverGap(elementData, fromIndex, toIndex); |
674 |
|
// checkInvariants(); |
675 |
|
} |
676 |
|
|
677 |
+ |
/** Erases the gap from lo to hi, by sliding down following elements. */ |
678 |
+ |
private void shiftTailOverGap(Object[] es, int lo, int hi) { |
679 |
+ |
System.arraycopy(es, hi, es, lo, size - hi); |
680 |
+ |
for (int to = size, i = (size -= hi - lo); i < to; i++) |
681 |
+ |
es[i] = null; |
682 |
+ |
} |
683 |
+ |
|
684 |
|
/** |
685 |
|
* A version of rangeCheck used by add and addAll. |
686 |
|
*/ |
766 |
|
w += end - r; |
767 |
|
throw ex; |
768 |
|
} finally { |
769 |
< |
final int oldSize = size, deleted = end - w; |
770 |
< |
modCount += deleted; |
766 |
< |
System.arraycopy(es, end, es, w, oldSize - end); |
767 |
< |
Arrays.fill(es, size -= deleted, oldSize, null); |
769 |
> |
modCount += end - w; |
770 |
> |
shiftTailOverGap(es, w, end); |
771 |
|
} |
772 |
|
} |
773 |
|
// checkInvariants(); |
775 |
|
} |
776 |
|
|
777 |
|
/** |
778 |
< |
* Save the state of the {@code ArrayList} instance to a stream (that |
779 |
< |
* is, serialize it). |
778 |
> |
* Saves the state of the {@code ArrayList} instance to a stream |
779 |
> |
* (that is, serializes it). |
780 |
|
* |
781 |
+ |
* @param s the stream |
782 |
+ |
* @throws java.io.IOException if an I/O error occurs |
783 |
|
* @serialData The length of the array backing the {@code ArrayList} |
784 |
|
* instance is emitted (int), followed by all of its elements |
785 |
|
* (each an {@code Object}) in the proper order. |
786 |
|
*/ |
787 |
|
private void writeObject(java.io.ObjectOutputStream s) |
788 |
< |
throws java.io.IOException{ |
788 |
> |
throws java.io.IOException { |
789 |
|
// Write out element count, and any hidden stuff |
790 |
|
int expectedModCount = modCount; |
791 |
|
s.defaultWriteObject(); |
804 |
|
} |
805 |
|
|
806 |
|
/** |
807 |
< |
* Reconstitute the {@code ArrayList} instance from a stream (that is, |
808 |
< |
* deserialize it). |
807 |
> |
* Reconstitutes the {@code ArrayList} instance from a stream (that is, |
808 |
> |
* deserializes it). |
809 |
> |
* @param s the stream |
810 |
> |
* @throws ClassNotFoundException if the class of a serialized object |
811 |
> |
* could not be found |
812 |
> |
* @throws java.io.IOException if an I/O error occurs |
813 |
|
*/ |
814 |
|
private void readObject(java.io.ObjectInputStream s) |
815 |
|
throws java.io.IOException, ClassNotFoundException { |
1556 |
|
for (i = beg; i < end; i++) |
1557 |
|
if (isClear(deathRow, i - beg)) |
1558 |
|
es[w++] = es[i]; |
1559 |
< |
final int oldSize = size; |
1551 |
< |
System.arraycopy(es, end, es, w, oldSize - end); |
1552 |
< |
Arrays.fill(es, size -= (end - w), oldSize, null); |
1559 |
> |
shiftTailOverGap(es, w, end); |
1560 |
|
// checkInvariants(); |
1561 |
|
return true; |
1562 |
|
} else { |