日期:2014-05-20 浏览次数:20766 次
import java.util.ArrayList; import java.util.HashSet; public class Test { public static void main(String[] args){ //init ArrayList<Integer> a1 = new ArrayList<Integer>(); ArrayList<Integer> a2 = new ArrayList<Integer>(); int[] arr1 = new int[]{1,2,3,4}; int[] arr2 = new int[]{1,2,5,6}; for(int i = 0;i < arr1.length;++i) a1.add(arr1[i]); for(int i = 0;i < arr2.length;++i) a2.add(arr2[i]); //get result ArrayList<Integer> result = findResult(a1, a2); System.out.println(result.toString()); } private static ArrayList<Integer> findResult(ArrayList<Integer> a1,ArrayList<Integer> a2){ @SuppressWarnings("unchecked") ArrayList<Integer> result = (ArrayList<Integer>) a1.clone(); result.removeAll(a2); a2.removeAll(a1); result.addAll(a2); return result; } }