List<String> list = null; for (String str : list) {//会报空指针异常 System.out.println(str); } List<String> list = new ArrayList();//正常运行 for (String str : list) { System.out.println(str); }
集合为null时会报空指针异常;长度为0的空集合不会;
List<String> list = null; for (String str : list) {//会报空指针异常 System.out.println(str); } List<String> list = new ArrayList();//正常运行 for (String str : list) { System.out.println(str); }
集合为null时会报空指针异常;长度为0的空集合不会;
转载于:https://www.cnblogs.com/licheng0201/p/10669629.html