1.使用iterator
public static void main(String[] args) { List<String> list=new ArrayList<String>(); list.add("han"); list.add("ji"); list.add("bao"); for(String s:list) { System.out.println(s); } System.out.println("................"); Iterator<String> it = list.iterator(); while(it.hasNext()) { String n = it.next(); if(n.equals("ji")) it.remove(); } for(String s:list) { System.out.println(s); } System.out.println("................"); }
2.使用removeAll
public static void main(String[] args) { List<String> list=new ArrayList<String>(); list.add("han"); list.add("ji"); list.add("bao"); for(String s:list) { System.out.println(s); } System.out.println("................"); List<String> list1=new ArrayList<String>(); for(String s:list) { if(s.equals("ji")) list1.add(s); } for(String s:list1) { System.out.println(s); } System.out.println("................"); list.removeAll(list1); for(String s:list) { System.out.println(s); } }