//removeAll()方法private static void testList(){ List<String> list = new ArrayList<String>(); List<String> list1 = new ArrayList<String>(); list.add("123"); list.add("456"); list1.add("123"); list.removeAll(list1); Iterator<String> iterator = list1.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } }
//输出结果 456
//clear()方法private static void testList(){ List<String> list = new ArrayList<String>(); List<String> list1 = new ArrayList<String>(); list.add("123"); list1.add("123"); list.add("456"); list.clear(); Iterator<String> iterator = list.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } if(list == null){ System.out.println("null"); }else if(list.size()==0){ System.out.println("list不为null"+"元素个数为"+list.size()); }else{ System.out.println("仍有元素"); } }
//输出结果 list不为null元素个数为0
时间: 2024-10-05 12:34:51