我有2个数组列表。我想返回两者之间的唯一值。这是怎么做到的?
String[] s1 = {10, 1};
String[] s2 = {10, 1, 13};
//loop through and compare element of s1 to s2
//switch varialbe used to indicate whether a match was found
boolean matchFound = false;
//outer loop for all the elements in s2
for (int i = 0; i < s2.lenght; i++) {
//inner loop for all the elements in s1
for (int i = 0; i < s1.lenght; i++) {
matchFound = true;
System.out.println("This " + s2[i] + "was found");
}
}
if(matchFound == false) {
System.out.println("This " + s2[i] + "was not found");
}
//set matchFound bool back to false
matchFound = false;
}
答案 0 :(得分:0)
我用正确答案更新了我的问题。
答案 1 :(得分:0)
如果您的数组没有重复值,或者您只对不在出现的元素感兴趣,那么您可以使用Set。
将数组转换为两组:
Set<T> one = new HashSet<T>(Arrays.asList(s1));
Set<T> two = = new HashSet<T>(Arrays.asList(s2));
one.removeAll(secondSet);
two.removeAll(firstSet);
如果两个集合都为空,则两个集合都相等,否则每个集合都具有唯一元素。