import java.util.*;public class TestList{
  public static void main(String args[]){
  List<String> li = new ArrayList<String>();    
    li.add("1");    
    li.add("2");    
    li.add("3");    
    li.add("4");    
    li.add("5");    
    li.add("6"); 
    for (String s : li) {    
        li.remove(s);    
    }
for (int i = 0; i < li.size(); i++) {    
System.out.println(li.get(i));    
}
    } 
}
用增强的for循环,为什么会抛出ConcurrentModificationException的异常?

解决方案 »

  1.   

    增强的for循环里面不能调remove()。如果要在loop里面remove:
    for(Iterator<String> it=li.iterator();it.hasNext();){
       it.next();
       it.remove();
    }
      

  2.   

    在遍历的时候不能改变ArrayList,在遍历的时候进行修改就会报这个错
    http://download.oracle.com/javase/1.5.0/docs/api/java/util/ConcurrentModificationException.html
    上面是java-doc上的说明
      

  3.   

    太久没用java,忘记了,但是下面这段应该可以回答楼主的问题了:
    The remove method removes the last element that was returned by next from the underlying Collection. The remove method may be called only once per call to next and throws an exception if this rule is violated.Note that Iterator.remove is the only safe way to modify a collection during iteration; the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress.Use Iterator instead of the for-each construct when you need to:    * Remove the current element. The for-each construct hides the iterator, so you cannot call remove. Therefore, the for-each construct is not usable for filtering.
        * Iterate over multiple collections in parallel. The following method shows you how to use an Iterator to filter an arbitrary Collection — that is, traverse the collection removing specific elements.    static void filter(Collection<?> c) {
            for (Iterator<?> it = c.iterator(); it.hasNext(); )
                if (!cond(it.next()))
                    it.remove();
        }
      

  4.   

    集合中 在遍历一个List时不能remove 。当遍历一个链表时,每个数的前后指针都是确定的 当remove一个数值时 可能使得这个链表断掉 从而造成遍历不能正常进行和结束 因此 在遍历时 是不允许remove的
      

  5.   

    服了你了··s是字符串··要移除的话是选择的移除··比如li.remove(int i)
    li.remove(s);    
      

  6.   

    要想改变list内容,用一般的for循环!
    增强for循环只能迭代,不可修改List内容,具体原因看源码,
    这是ArrayList中的add方法实现的源码,内部实现是采用数组实现的!而不是采用链表!
      public boolean add(E e) {
    ensureCapacity(size + 1);  // Increments modCount!!
    elementData[size++] = e;
    return true;
        }
      

  7.   

    List是不能直接remove的。如果需要移除用迭代器!
      

  8.   


    import java.util.*;public class TestList {
    public static void main(String args[]) {
    List<String> li = new ArrayList<String>();
    li.add("1");
    li.add("2");
    li.add("3");
    li.add("4");
    li.add("5");
    li.add("6");
    for (String s : li) {
    if(s.equals("5")) //这是只能是最后第二个元素的值("5")
    li.remove(2);//这里移除哪个元素都可以
    }
    for (int i = 0; i < li.size(); i++) {
    System.out.println(li.get(i));
    }
    }
    }}
    不知道有没有人能解释上面的现象。
      

  9.   

    你把List里面的东西都给remove掉了的
      

  10.   

    迭代的时候不能直接 remove,一种办法是用 iterator.remove,ConcurrentModificationException 是说,当迭代器在跑循环时,有人把集合改了,这是不允许的,文档中明确列出了这种异常。JCF 中说明了 ArrayList 不是一个线程集合,它不使用同步机制确保数据修改操作正确有序,但是会在迭代操作时设置版本号,在迭代器创建时保留一个版本号,每次移动指针,都要对比版本号,如果有人改了就报错,因为这不符合约定的规则,结果是不可预料的。设计的考虑是:如果你不确信它是正确的,那就报错停下来,让程序员去修正这个不应该出现的情况,我们不假设用什么办法能保证数据正确,因为为了性能考虑我们不保证线程安全,如果你想要线程安全就换用 Vector 之类的集合。这就像我们看到 C/C++ 的书里面介绍某些情况如果条件不符合要求的时候,结果是“未定义的”,我们这样做是明确列出“未定义的”行为需要程序员去纠正,API 不会做任何假设,它的好处是防止软件复杂性,如果随意处理未定义的行为,大型软件的复杂性就无法控制而且很难调试找到原因。
      

  11.   


    for(Iterator<String> it=li.iterator();it.hasNext();){
       it.next();
       it.remove();
    }
    就是这样,别无它发。
      

  12.   

    我一般都是用普通的for循环!很少用增强的!才发现还有这种说法!学习了!
      

  13.   


    我也F你了, 你不知道 remove 被有 remove(Object o) 这个方法吗?
      

  14.   

    在循环的时候不能修改 list,set,map 等集合的值。
    如果一定要修改。就用Iterator.remove();
      

  15.   

    List list=new ArrayList();
    list.remove(int index )//可以移除索引
    list.remove(Object o)//也可以移除对象
    15楼是对的,这样才不会报错
      

  16.   

    一般来说,增强型for循环只适用于查询日常操作,如果你要修改只能用一般的for循环,或者引入迭代器
      

  17.   

    哈哈 我博客上的问题http://blog.csdn.net/ScAREcrOw_ss/archive/2010/10/21/5957300.aspx
      

  18.   

    这个问题为什么会报这种异常,其实想想也不难发现。
    ArrayList底层是数组实现的,假设现在有a[0]=1,a[1]=2,a[2]=3
    用for循环遍历的时候 
    for(int i=0;i<a.length;i++){
    }
    假设遍历到第二个数  也就是a[1]的时候,删除了a[1]   ArrayList的底层是将a[1]后面的数向前移动一个位置,由于下标改了  i没改   这样就会发生一些问题  所以就出现在遍历的时候改变整个数组  就干脆抛异常了用迭代的方式则没这个问题
      

  19.   

    遍历的时候不能改变ArrayList,在遍历的时候进行修改就会报这个错
      

  20.   

    唉,看了那么多的回复,很多都是误解!
    谁说list遍历进行remove?正序是不行的,因为移除后下一个迭代序号永远比你拥有的要大,反序就可以!
      

  21.   

    1.倒序删
    2.用迭代器删
    3.用一个临时的tmpList保存要删的东东,for完一起removeAll