public static void main(String[] args) { //异常代码 /*List<String> a = new ArrayList<String>(); a.add("1"); a.add("2"); for (String temp : a) { if ("2".equals(temp)) { a.remove(temp); } System.out.println(temp); }*/ //正常代码 List<String> a = new ArrayList<String>(); a.add("1"); a.add("2"); //迭代器 Iterator<String> iterator = a.iterator(); while (iterator.hasNext()){ String temp = iterator.next(); if(temp == "2"){ iterator.remove(); } } System.out.println("______________"); System.out.println(a.toString()); }