例如有如下2个list:
list1{1,2,3,4,4,5}
list2{3,3,4,5,6,6,7}程序运行后要求两个list内容如下
list1{1,2}
list2{6,6,7}我只能想到一个记住下标,然后挨个remove(index)的方法,觉得太笨了,还有什么好方法吗??
list1{1,2,3,4,4,5}
list2{3,3,4,5,6,6,7}程序运行后要求两个list内容如下
list1{1,2}
list2{6,6,7}我只能想到一个记住下标,然后挨个remove(index)的方法,觉得太笨了,还有什么好方法吗??
List<String> l2 = new ArrayList<String>();
l1.add("1");
l1.add("2");
l1.add("3");
l1.add("4");
l1.add("4");
l1.add("5");
l2.add("3");
l2.add("3");
l2.add("4");
l2.add("5");
l2.add("6");
l2.add("6");
l2.add("7");
Object[] o = Arrays.copyOf(l1.toArray(), l1.size());
l1.removeAll(l2);
List<Object> l3 = Arrays.asList(o);
l2.removeAll(l3);
System.out.println(l1);
System.out.println(l2);
List listTwo=new ArrayList<Integer>(Arrays.asList(3,3,4,5,6,6,7));
listOne.removeAll(listTwo);
System.out.println(listOne);
//[1, 2]这样就可以了!
public static void main(String[] a) throws IOException {
Integer[] a1 = new Integer[]{1,2,3,4,4,5};
Integer[] a2 = new Integer[]{3,3,4,5,6,6,7};
List<Integer> list1 = new ArrayList<Integer>();
List<Integer> list2 = new ArrayList<Integer>();
List<Integer> list3 = new ArrayList<Integer>();
Collections.addAll(list1, a1);
Collections.addAll(list3, a1);
Collections.addAll(list2, a2);
list3.retainAll(list2);
list1.removeAll(list3);
list2.removeAll(list3);
System.out.println(list1);
System.out.println(list2);
}