--- jsr166/src/main/java/util/Vector.java 2016/11/30 03:31:47 1.37 +++ jsr166/src/main/java/util/Vector.java 2016/12/05 00:08:01 1.40 @@ -306,9 +306,9 @@ public class Vector modCount++; if (newSize > elementData.length) grow(newSize); - for (int i = newSize; i < elementCount; i++) - elementData[i] = null; - elementCount = newSize; + final Object[] es = elementData; + for (int to = elementCount, i = elementCount = newSize; i < to; i++) + es[i] = null; } /** @@ -676,9 +676,10 @@ public class Vector * method (which is part of the {@link List} interface). */ public synchronized void removeAllElements() { - Arrays.fill(elementData, 0, elementCount, null); + final Object[] es = elementData; + for (int to = elementCount, i = elementCount = 0; i < to; i++) + es[i] = null; modCount++; - elementCount = 0; } /** @@ -1028,7 +1029,8 @@ public class Vector for (i = beg; i < end; i++) if (isClear(deathRow, i - beg)) es[w++] = es[i]; - Arrays.fill(es, elementCount = w, end, null); + for (i = elementCount = w; i < end; i++) + es[i] = null; // checkInvariants(); return true; } else { @@ -1159,20 +1161,26 @@ public class Vector * (If {@code toIndex==fromIndex}, this operation has no effect.) */ protected synchronized void removeRange(int fromIndex, int toIndex) { - final Object[] es = elementData; - final int oldSize = elementCount; - System.arraycopy(es, toIndex, es, fromIndex, oldSize - toIndex); - modCount++; - Arrays.fill(es, elementCount -= (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, elementCount - hi); + for (int to = elementCount, i = (elementCount -= hi - lo); i < to; i++) + es[i] = null; + } + /** - * Save the state of the {@code Vector} instance to a stream (that - * is, serialize it). + * Saves the state of the {@code Vector} instance to a stream + * (that is, serializes it). * This method performs synchronization to ensure the consistency * of the serialized data. + * + * @param s the stream + * @throws java.io.IOException if an I/O error occurs */ private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { @@ -1452,26 +1460,15 @@ public class Vector @SuppressWarnings("unchecked") public void forEachRemaining(Consumer action) { - int i, hi; // hoist accesses and checks from loop - Object[] a; if (action == null) throw new NullPointerException(); - if ((hi = fence) < 0) { - synchronized (Vector.this) { - expectedModCount = modCount; - a = array = elementData; - hi = fence = elementCount; - } - } - else - a = array; - if (a != null && (i = index) >= 0 && (index = hi) <= a.length) { - while (i < hi) - action.accept((E) a[i++]); - if (modCount == expectedModCount) - return; - } - throw new ConcurrentModificationException(); + final int hi = getFence(); + final Object[] a = array; + int i; + for (i = index, index = hi; i < hi; i++) + action.accept((E) a[i]); + if (modCount != expectedModCount) + throw new ConcurrentModificationException(); } public long estimateSize() {