public static void main(String[] args) { List<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c", "d")); List<String> list1 = new ArrayList<String>(Arrays.asList("a", "b", "c", "d")); Test1 t = new Test1(); for (String s : t.removeByIterator(list, "c")) { System.out.print(s); } System.out.println(); System.out.println("----------------------------"); for (String s : t.removeByNewList(list1, "c")) { System.out.print(s); } } public List<String> removeByNewList(List<String> list, String str) { List<String> newList = new ArrayList<String>(); for (String s : list) { if (!s.equals(str)) { newList.add(s); } } return newList; } public List<String> removeByIterator(List<String> list, String str) { Iterator<String> iterator = list.iterator(); while (iterator.hasNext()) { String s = iterator.next(); if (s.equals(str)) { // 移除迭代器返回的元素 iterator.remove(); } } return list; } |