java.util.ConcurrentModificationException的解决方案
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); } }
posted on 2013-11-14 16:57 passer1991 阅读(186) 评论(0) 编辑 收藏 举报