--- jsr166/src/main/java/util/ArrayList.java 2016/11/14 22:46:22 1.43 +++ jsr166/src/main/java/util/ArrayList.java 2016/11/16 00:17:25 1.44 @@ -912,25 +912,21 @@ public class ArrayList extends Abstra } @Override - @SuppressWarnings("unchecked") - public void forEachRemaining(Consumer consumer) { - Objects.requireNonNull(consumer); + public void forEachRemaining(Consumer action) { + Objects.requireNonNull(action); final int size = ArrayList.this.size; int i = cursor; - if (i >= size) { - return; - } - final Object[] elementData = ArrayList.this.elementData; - if (i >= elementData.length) { - throw new ConcurrentModificationException(); + if (i < size) { + final Object[] es = elementData; + if (i >= es.length) + throw new ConcurrentModificationException(); + for (; i < size && modCount == expectedModCount; i++) + action.accept(elementAt(es, i)); + // update once at end to reduce heap write traffic + cursor = i; + lastRet = i - 1; + checkForComodification(); } - while (i != size && modCount == expectedModCount) { - consumer.accept((E) elementData[i++]); - } - // update once at end of iteration to reduce heap write traffic - cursor = i; - lastRet = i - 1; - checkForComodification(); } final void checkForComodification() { @@ -1191,25 +1187,21 @@ public class ArrayList extends Abstra return (E) elementData[offset + (lastRet = i)]; } - @SuppressWarnings("unchecked") - public void forEachRemaining(Consumer consumer) { - Objects.requireNonNull(consumer); + public void forEachRemaining(Consumer action) { + Objects.requireNonNull(action); final int size = SubList.this.size; int i = cursor; - if (i >= size) { - return; - } - final Object[] elementData = root.elementData; - if (offset + i >= elementData.length) { - throw new ConcurrentModificationException(); + if (i < size) { + final Object[] es = root.elementData; + if (offset + i >= es.length) + throw new ConcurrentModificationException(); + for (; i < size && modCount == expectedModCount; i++) + action.accept(elementAt(es, offset + i)); + // update once at end to reduce heap write traffic + cursor = i; + lastRet = i - 1; + checkForComodification(); } - while (i != size && modCount == expectedModCount) { - consumer.accept((E) elementData[offset + (i++)]); - } - // update once at end of iteration to reduce heap write traffic - cursor = i; - lastRet = i - 1; - checkForComodification(); } public int nextIndex() {