0

我正在尝试使用制作一个方法,该方法接受两个字符串数组列表并输出一个列表中的任何一个列表中的值列表,但不能同时输出,不允许重复。

这是我到目前为止所拥有的,但我没有通过 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" ));
}
4

3 回答 3

2

你的逻辑有点不对劲。

集合并集:{ A, B } ∪ { B, C } = { A, B, C } [所有不重复的元素]
集合交集:{ A, B } ∩ { B, C } = { B } [ 共同元素] 设置差异:{ A, B } / { B, C } = { A } [注意,没有 C 元素]

你想要设置联合- 设置交集:(对称差异)这里归功于菲尔

  1. {“palpatine”、“dooku”、“vader”、“sidius”}
  2. {“padme”、“vader”、“sidius”、“ackbar”、“padme”}

( 1 ∪ 2 / 1 ∩ 2 )

{ palpatine dooku vader sidius padme ackbar } - { padme vader } = { palpatine dooku sidius ackbar }

图像示例

replaceAll 应用集:( oneSet 差 twoSet )

跳闸来自removeAll方法。它从两个中删除所有在一个中找到的元素,但不会将所有从一中找不到的元素添加到

以下代码通过执行2 个集合差异和 1 个并集来修复您的测试,以实现对称差异/异或。

异或(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);
}
于 2013-09-25T20:49:27.310 回答
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;
}
于 2013-09-25T20:46:49.890 回答
0

好吧,这里最简单的解决方案是遍历两个列表,检查单词是否存在于另一个列表中,如果不存在,则将其添加到集合中。

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++;
}
于 2013-09-25T20:35:37.573 回答