--- jsr166/src/main/java/util/ArrayList.java 2016/11/30 03:31:47 1.45 +++ jsr166/src/main/java/util/ArrayList.java 2016/12/08 05:02:32 1.48 @@ -578,8 +578,9 @@ public class ArrayList extends Abstra */ public void clear() { modCount++; - Arrays.fill(elementData, 0, size, null); - size = 0; + final Object[] es = elementData; + for (int to = size, i = size = 0; i < to; i++) + es[i] = null; } /** @@ -669,13 +670,17 @@ public class ArrayList extends Abstra outOfBoundsMsg(fromIndex, toIndex)); } modCount++; - final Object[] es = elementData; - final int oldSize = size; - System.arraycopy(es, toIndex, es, fromIndex, oldSize - toIndex); - Arrays.fill(es, size -= (toIndex - fromIndex), oldSize, null); + shiftTailOverGap(elementData, fromIndex, toIndex); // checkInvariants(); } + /** Erases the gap from lo to hi, by sliding down following elements. */ + private void shiftTailOverGap(Object[] es, int lo, int hi) { + System.arraycopy(es, hi, es, lo, size - hi); + for (int to = size, i = (size -= hi - lo); i < to; i++) + es[i] = null; + } + /** * A version of rangeCheck used by add and addAll. */ @@ -761,10 +766,8 @@ public class ArrayList extends Abstra w += end - r; throw ex; } finally { - final int oldSize = size, deleted = end - w; - modCount += deleted; - System.arraycopy(es, end, es, w, oldSize - end); - Arrays.fill(es, size -= deleted, oldSize, null); + modCount += end - w; + shiftTailOverGap(es, w, end); } } // checkInvariants(); @@ -772,15 +775,17 @@ public class ArrayList extends Abstra } /** - * Save the state of the {@code ArrayList} instance to a stream (that - * is, serialize it). + * Saves the state of the {@code ArrayList} instance to a stream + * (that is, serializes it). * + * @param s the stream + * @throws java.io.IOException if an I/O error occurs * @serialData The length of the array backing the {@code ArrayList} * instance is emitted (int), followed by all of its elements * (each an {@code Object}) in the proper order. */ private void writeObject(java.io.ObjectOutputStream s) - throws java.io.IOException{ + throws java.io.IOException { // Write out element count, and any hidden stuff int expectedModCount = modCount; s.defaultWriteObject(); @@ -799,8 +804,12 @@ public class ArrayList extends Abstra } /** - * Reconstitute the {@code ArrayList} instance from a stream (that is, - * deserialize it). + * Reconstitutes the {@code ArrayList} instance from a stream (that is, + * deserializes it). + * @param s the stream + * @throws ClassNotFoundException if the class of a serialized object + * could not be found + * @throws java.io.IOException if an I/O error occurs */ private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { @@ -1362,6 +1371,9 @@ public class ArrayList extends Abstra } } + /** + * @throws NullPointerException {@inheritDoc} + */ @Override public void forEach(Consumer action) { Objects.requireNonNull(action); @@ -1513,6 +1525,9 @@ public class ArrayList extends Abstra return (bits[i >> 6] & (1L << i)) == 0; } + /** + * @throws NullPointerException {@inheritDoc} + */ @Override public boolean removeIf(Predicate filter) { return removeIf(filter, 0, size); @@ -1547,9 +1562,7 @@ public class ArrayList extends Abstra for (i = beg; i < end; i++) if (isClear(deathRow, i - beg)) es[w++] = es[i]; - final int oldSize = size; - System.arraycopy(es, end, es, w, oldSize - end); - Arrays.fill(es, size -= (end - w), oldSize, null); + shiftTailOverGap(es, w, end); // checkInvariants(); return true; } else {