标签:
开发中,常有“遍历集合,依次判断是否符合条件,如符合条件则删除当前元素”的场景,有一些陷阱常犯。
import java.util.ArrayList; import java.util.List; public class ListTest_Unwork { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("1"); list.add("2"); list.add("3"); list.add("4"); list.add("5"); System.out.println("Original list : " + list); String temp = null; for (int i = 0; i < list.size(); i++) { temp = list.get(i); System.out.println("Check for " + temp); if ("3".equals(temp)) { list.remove(temp); } } System.out.println("Removed list : " + list); } }
日志打印:
Original list : [1, 2, 3, 4, 5] Check for 1 Check for 2 Check for 3 Check for 5 Removed list : [1, 2, 4, 5]
如日志所见,其中值为4的元素并未经过判断,漏网之鱼。
对于此情况,我一般都从后面开始遍历,以避免问题:
import java.util.ArrayList; import java.util.List; public class ListTest_Work { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("1"); list.add("2"); list.add("3"); list.add("4"); list.add("5"); System.out.println("Original list : " + list); System.out.println(); String temp = null; for (int i = list.size() - 1; i >= 0; i--) { temp = list.get(i); System.out.println("Check for " + temp); if ("3".equals(temp)) { list.remove(temp); } } System.out.println("Removed list : " + list); } }
或直接新开一个list,重新摆放,但浪费内存,慎用:
import java.util.ArrayList; import java.util.List; public class ListTest_Work2 { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("1"); list.add("2"); list.add("3"); list.add("4"); list.add("5"); System.out.println("Original list : " + list); System.out.println(); List<String> tempList = new ArrayList<String>(); for (String temp : list) { System.out.println("Check for " + temp); if (!"3".equals(temp)) { tempList.add(temp); } } System.out.println("Removed list : " + tempList); } }
用Iterator方式或简写的for(Object o : list) {}方式,遍历集合,修改元素时会报异常,具体见关于List的ConcurrentModificationException
import java.util.ArrayList; import java.util.List; public class ListTest2_Unwork { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("1"); list.add("2"); list.add("3"); list.add("4"); list.add("5"); System.out.println("Original list : " + list); System.out.println(); for (String temp : list) { System.out.println("Check for " + temp); if ("3".equals(temp)) { list.remove(temp); } } System.out.println("Removed list : " + list); } }
或
import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class ListTest3_Unwork { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("1"); list.add("2"); list.add("3"); list.add("4"); list.add("5"); System.out.println("Original list : " + list); System.out.println(); Iterator<String> i = list.iterator(); String temp = null; while (i.hasNext()) { temp = i.next(); System.out.println("Check for " + temp); if ("3".equals(temp)) { list.remove(temp); } } System.out.println("Removed list : " + list); } }
日志:
Original list : [1, 2, 3, 4, 5] Check for 1 Check for 2 Check for 3 Exception in thread "main" java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859) at java.util.ArrayList$Itr.next(ArrayList.java:831) at ListTest3_Unwork.main(ListTest3_Unwork.java:20)
在删除元素“3”时,会报异常。
对于此情况,需要用iterator的remove方法替代:
import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class ListTest3_Work { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("1"); list.add("2"); list.add("3"); list.add("4"); list.add("5"); System.out.println("Original list : " + list); System.out.println(); Iterator<String> i = list.iterator(); String temp = null; while (i.hasNext()) { temp = i.next(); System.out.println("Check for " + temp); if ("3".equals(temp)) { i.remove(); } } System.out.println("Removed list : " + list); } }
标签:
原文地址:http://www.cnblogs.com/nick-huang/p/4635670.html