ConcurrentModificationExceptionTest
//Iterator迭代Collection的时候,不要用它们的API直接修改集合的内容,如果要修改可以用Iterator的remove()方法,例如:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ConcurrentModificationExceptionTest
{
  public static void main(String[] args) {
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");

for (Iterator it = list.iterator();
  it.hasNext();) {
  String str = (String) it.next();
  if (str.equals("a")) {
    // 在运行期会报java.util.
    //ConcurrentModificationException
    list.remove(str);
  }
}

// for (Iterator it = list.iterator();
// it.hasNext();) {
// String str = (String) it.next();
// if (str.equals("a")) {
// // 正常
// it.remove();
// }
// }
  }
}
irini   2007-04-19 22:48:02 评论:0   阅读:38   引用:0

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

Copyright@2008 powered by YuLog