我正在尝试使用make一个方法,该方法接收两个字符串数组列表,并输出列表中任何一个列表中的值列表,但不能同时输出这两个值,不允许重复。
这是我迄今为止所做的,但我没有通过3次junit测试
public static ArrayList<String> getDifference(ArrayList<String> one, ArrayList<String> two)
{
Set<String> oneSet = new LinkedHashSet<>(one);
ArrayList<String> finalone = new ArrayList<>(oneSet);
Set<String> twoSet = new LinkedHashSet<>(two);
ArrayList<String> finaltwo = new ArrayList<>(twoSet);
Collection<String> result = new ArrayList<String>(finaltwo);
result.removeAll(finalone);
ArrayList<String> list = new ArrayList<String>(result);
return list;
}
我失败的测试低于任何帮助,我可以做些什么来解决这个问题,我们将不胜感激,并提前感谢你。
@Test
public void testGetDifferenceWithEmptyListSecond() {
String[] un = { "luke", "leah", "han" };
String[] duex = { };
ArrayList<String> one = new ArrayList<String>( Arrays.asList( un ));
ArrayList<String> two = new ArrayList<String>( Arrays.asList( duex ));
ArrayList<String> actual = Lab03Two.getDifference( one, two );
assertEquals( "The number of elements is incorrect", 3, actual.size() );
assertTrue ( "The value \"luke\" was not found in the result", actual.contains( "luke" ));
assertTrue ( "The value \"leah\" was not found in the result", actual.contains( "leah" ));
assertTrue ( "The value \"han\" was not found in the result", actual.contains( "han" ));
}
@Test
public void testGetDifferenceWithOverlapAndDuplicates() {
String[] un = { "palpatine", "dooku", "vader", "sidius" };
String[] duex = { "padme", "vader", "sidius", "ackbar", "padme" };
ArrayList<String> one = new ArrayList<String>( Arrays.asList( un ));
ArrayList<String> two = new ArrayList<String>( Arrays.asList( duex ));
ArrayList<String> actual = Lab03Two.getDifference( one, two );
assertEquals( "The number of elements is incorrect", 4, actual.size() );
assertTrue ( "The value \"ackbar\" was not found in the result", actual.contains( "ackbar" ));
assertTrue ( "The value \"dooku\" was not found in the result", actual.contains( "dooku" ));
assertTrue ( "The value \"padme\" was not found in the result", actual.contains( "padme" ));
assertTrue ( "The value \"palpatine\" was not found in the result", actual.contains( "palpatine" ));
}
}
@Test
public void testGetDifferenceWithNoOverlap() {
String[] un = { "obi-wan", "jar-jar", "anakin" };
String[] duex = { "r2-d2", "c-3po" };
ArrayList<String> one = new ArrayList<String>( Arrays.asList( un ));
ArrayList<String> two = new ArrayList<String>( Arrays.asList( duex ));
ArrayList<String> actual = Lab03Two.getDifference( one, two );
assertEquals( "The number of elements is incorrect", 5, actual.size() );
assertTrue ( "The value \"obi-wan\" was not found in the result", actual.contains( "obi-wan" ));
assertTrue ( "The value \"jar-jar\" was not found in the result", actual.contains( "jar-jar" ));
assertTrue ( "The value \"anakin\" was not found in the result", actual.contains( "anakin" ));
assertTrue ( "The value \"r2-d2\" was not found in the result", actual.contains( "r2-d2" ));
assertTrue ( "The value \"c-3po\" was not found in the result", actual.contains( "c-3po" ));
}
答案 0 :(得分:2)
你的逻辑有点偏。
设置联盟:{A,B}∪{B,C} = {A,B,C} [所有没有复制的元素]
设置交叉点:{A,B}∩{B,C} = {B} [常用元素]
设置差异:{A,B} / {B,C} = {A} [通知,无C元素]
你想要设置联盟 - 设置交集 :(对称差异)信用这里归功于Phil
(1∪2/1∩2)
{palpatine dooku vader sidius padme ackbar} - {padme vader} = {palpatine dooku sidius ackbar}
replaceAll应用set 差异 :( oneSet difference twoSet)
旅程来自 removeAll 方法。它会从一个中删除两个所有元素,但不会添加一个中不的所有元素>到两个。
以下代码通过执行 2 set difference和1 union 来修复测试,以实现对称差异/独占或。
独家或(xor),对称差异
public static List<String> getXOR(List<String> oneArray, List<String> twoArray) {
Set<String> oneSet = new HashSet<>(oneArray);
Set<String> twoSet = new HashSet<>(twoArray);
oneSet.removeAll(twoArray);// 1. oneSet / twoArray , oneSet !AND twoArray
twoSet.removeAll(oneArray);// 2. twoSet / oneArray , twoSet !AND oneArray
oneSet.addAll(twoSet); // 3. oneSet U twoSet , oneSet OR twoSet
return new ArrayList<String>(oneSet);
}
答案 1 :(得分:1)
public static ArrayList<String> getDifference(ArrayList<String> one, ArrayList<String> two)
{
ArrayList<String> list = new ArrayList<String>();
//iterate over all elements of one
//if two does not contain it, it's a difference -> add it
for (String i : one) {
if (!two.contains(i)) {
set.add(i);
}
}
//same with two
for (String i : two) {
if (!one.contains(i)) {
set.add(i);
}
}
return list;
}
答案 2 :(得分:0)
嗯,这里最简单的解决方案是遍历两个列表,检查单词是否存在于另一个列表中,如果不存在,则将其添加到Set中。
Set<String> solution = new LinkedHashSet<>();
int n1 = one.size();
int n2 = two.size();
int i=0;
int j=0;
while (i<n1) {
while (j<n2) {
if (!one.get(i).equals(two.get(j)))
solution.add(one.get(i));
j++;
}
if (i>=n2) {
solution.add(one.get(i));
}
i++;
}
while (j<n2) {
solution.add(two.get(j));
j++;
}