--- jsr166/src/jdk8/java/util/ArrayList.java 2016/11/16 19:46:20 1.2 +++ jsr166/src/jdk8/java/util/ArrayList.java 2018/05/07 23:38:48 1.3 @@ -417,6 +417,11 @@ public class ArrayList extends Abstra return (E) elementData[index]; } + @SuppressWarnings("unchecked") + static E elementAt(Object[] es, int index) { + return (E) es[index]; + } + /** * Returns the element at the specified position in this list. * @@ -1435,18 +1440,19 @@ public class ArrayList extends Abstra } @Override - @SuppressWarnings("unchecked") public void replaceAll(UnaryOperator operator) { + replaceAllRange(operator, 0, size); + } + + private void replaceAllRange(UnaryOperator operator, int i, int end) { Objects.requireNonNull(operator); final int expectedModCount = modCount; - final int size = this.size; - for (int i=0; modCount == expectedModCount && i < size; i++) { - elementData[i] = operator.apply((E) elementData[i]); - } - if (modCount != expectedModCount) { + final Object[] es = elementData; + for (; modCount == expectedModCount && i < end; i++) + es[i] = operator.apply(elementAt(es, i)); + if (modCount != expectedModCount) throw new ConcurrentModificationException(); - } - modCount++; + // checkInvariants(); } @Override