java操作数组转list集合删除元素报错ConcurrentModificationException

public static void main(String[] args) {
ArrayList
<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c", "d")); for (int i = 0; i < list.size(); i++) { list.remove(i); } System.out.println(list); }
输出:[b, d]
  public static void main(String[] args) {

        ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c", "d"));
        for (String s : list) {
            if (s.equals("a"))
                list.remove(s);
        }
        
    }
报错ConcurrentModificationException
 public static void main(String[] args) {

        ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c", "d"));
        Iterator<String> iter = list.iterator();
        while (iter.hasNext()) {
            String s = iter.next();

            if (s.equals("a")) {
                iter.remove();
            }
        }
}
 
输出:[b, c, d]
posted @ 2020-01-04 21:37  天葬  阅读(558)  评论(0编辑  收藏  举报