changeset 6449:17888810ebe5

Improve implementation of ArrayList.forEach
author briangoetz
date Tue, 20 Nov 2012 12:44:00 -0500
parents 8a58ecf4853e
children bd50944b07fa efc91616a15b
files src/share/classes/java/util/ArrayList.java
diffstat 1 files changed, 4 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/ArrayList.java	Tue Nov 20 11:55:41 2012 -0500
+++ b/src/share/classes/java/util/ArrayList.java	Tue Nov 20 12:44:00 2012 -0500
@@ -1140,12 +1140,13 @@
 
     @Override
     public void forEach(Block<? super E> block) {
+        int expectedModCount = modCount;
         E[] elementData = (E[]) this.elementData;
         int size = this.size;
-        if (size > elementData.length)
+        for (int i=0; modCount == expectedModCount && i<size; i++)
+            block.accept(elementData[i]);
+        if (modCount != expectedModCount)
             throw new ConcurrentModificationException();
-        for (int i=0; i<size; i++)
-            block.accept(elementData[i]);
     }
 
     @Override