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]
原文地址:https://www.cnblogs.com/bigsiji/p/12150500.html
时间: 2024-10-06 23:55:34