Java - ArrayList List 等迭代集合执行移除(remove) 的正确方法
方法1:
List<String> al = new ArrayList<String>(); Iterator<String> it = al.iterator(); while (it.hasNext()) { if ("1".equals(it.next())) it.remove(); }
方法2:
List<String> al = new ArrayList<String>(); for (int i = 0; i < al.size(); i++) { if ("1".equals(al.get(i))) { al.remove(i); i--; } }