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]
哇!又赚了一天人民币