我有两个数组
String[] ID1={"19","20","12","13","14"};
String[] ID2={"10","11","12","13","15"};
在比较上面两个数组时,如何得到以下答案。
我想在比较上面两个数组时排除常见元素。
String[] Result={"14","15","19","20","10","11"};
答案 0 :(得分:7)
好像你想要两个联合(没有重复,对吗?)减去交集:
Set<Integer> union = new HashSet<Integer>(Arrays.asList(ID1));
union.addAll(Arrays.asList(ID2);
Set<Integer> intersection = new HashSet<Integer>(Arrays.asList(ID1));
intersection.retainAll(Arrays.asList(ID2);
union.removeAll(intersection);
// result is left in "union" (which is badly named now)
(我将你的String更改为Integer,这似乎更适合数据,但它可以使用相同的方式使用String)
答案 1 :(得分:2)
看起来像XOR操作;)
请直接描述您的需求。伪代码:
foreach s in ID1 {
if(ID2.contains(s)) {
ID2.remove(s)
} else {
ID2.add(s)
}
}
ID2将包含您的结果。假设两个数组都没有重复。