Duplicate :
|
|
Duplicate :
|
|
Relates :
|
|
Relates :
|
Name: gm110360 Date: 08/06/2003 FULL PRODUCT VERSION : Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01) Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode) FULL OPERATING SYSTEM VERSION : Microsoft Windows 2000 [Version 5.00.2195] A DESCRIPTION OF THE PROBLEM : Concurrent modification exception is not thrown on the second to last iteration of an iterator. STEPS TO FOLLOW TO REPRODUCE THE PROBLEM : 1. Compile and run the small test program -- it should die with concurrent modification exception. If the item removed from the list is anything but "c" it works. EXPECTED VERSUS ACTUAL BEHAVIOR : Expected a concurrent modification exception in this case: Iterator iter = list.iterator(); while(iter.next()) { Object o = iter.next(); if(2nd to last item in list) { list.remove(o); // <-- expected exception here! } } ERROR MESSAGES/STACK TRACES THAT OCCUR : the problem is the lack of an exception REPRODUCIBILITY : This bug can be reproduced always. ---------- BEGIN SOURCE ---------- import java.util.Iterator; import java.util.ArrayList; import java.util.List; import java.util.Vector; /** * @author Steven R Brandt */ public class CopyIterator { public void remove() { throw new Error("cannot remove from CopyIterator"); } public static void main(String[] args) throws Exception { list.add(new ToyListener("a")); list.add(new ToyListener("b")); list.add(new ToyListener("c")); list.add(new ToyListener("d")); Iterator iter = list.iterator(); while(iter.hasNext()) { ToyListener toy = (ToyListener)iter.next(); toy.event(); System.out.println(toy); } } static List list = new Vector(); public static class ToyListener { String name; ToyListener(String name) { this.name = name; } public void event() { // this should cause a concurrent modification exception // if "a", "b", or "d" are used it will if(name.equals("c")) { list.remove(this); } } public String toString() { return "Toy: "+name; } } } ---------- END SOURCE ---------- CUSTOMER WORKAROUND : write your own iterator (Incident Review ID: 180003) ======================================================================
|