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

public static void main(String[] args) {
ArrayList
list = new ArrayList(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 list = new ArrayList(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 list = new ArrayList(Arrays.asList("a", "b", "c", "d"));
        Iterator iter = list.iterator();
        while (iter.hasNext()) {
            String s = iter.next();

            if (s.equals("a")) {
                iter.remove();
            }
        }
}
 
输出:[b, c, d]

你可能感兴趣的:(java操作数组转list集合删除元素报错ConcurrentModificationException)