检查数组列表与另一个数组列表

时间:2013-10-24 07:42:24

标签: java arrays loops for-loop boolean

这是拼写检查程序中的一种方法。正如标题所解释的那样,当且仅当在父数组中找到添加到arraylist的所有单词时,它才会返回true。否则它应该返回一个假值。我已经和它斗争了几个小时,这是我目前的情况......

    /**
    * This method returns true if (and only if) all words in the
    * given wordList are found in the dictionary.
    */
    public boolean allKnown(ArrayList<String> wordList)
    {
        boolean result = true;
        for(int index = 0; index < wordList.size(); index++)
        {
            if(words.contains(!wordList.contains(index)))
            {
                result = false;
            }
        result = true;
        }
    return result;
    }

所有我真正需要的是一种结果是或否的方法,但我迷失了。 请尝试使用给出的代码,因为这是一个教授该代码的练习。 谢谢!

7 个答案:

答案 0 :(得分:2)

你的问题在这里:

if(words.contains(!wordList.contains(index)))

!wordList.contains(index)是一个布尔表达式,因此它始终计算为truefalse。所以你实际上是在检查words列表是否包含true或false,而不是你想要的单词。将其替换为if(!words.contains(wordList.get(index))以检查字典中是否找到当前单词。

我建议采用以下解决方案:逐字逐句wordList,并检查每个单词是否在字典中找到。如果不是这样,立即返回false。如果到达循环的末尾,则返回true。

答案 1 :(得分:2)

这可能是另一种解决方案:

public static boolean allKnown(List<String> parent, List<String> child) {
    List<String> temp = new ArrayList<String>(child);
    temp.removeAll(parent);
    return temp.isEmpty();
}

例如:

List<String> parent = Arrays.asList("w1", "w2", "w3", "w4");
List<String> childOk = Arrays.asList("w1", "w4");
List<String> childKo = Arrays.asList("w1", "xx");
System.out.println(allKnown(parent, childOk));
System.out.println(allKnown(parent, childKo));

打印:

true
false

答案 2 :(得分:1)

取出result = true; - 您不希望在循环的每一步将值重置为true

同时将wordList.contains更改为wordList.get(因为您希望在特定索引处获取该字词,而不是检查它是否包含在wordList中)并移出! (因为你不能'不'是一个字符串)。

您还可以通过在for-loop条件中检查result的值来进行优化(或者直接在if语句中直接返回)。

public boolean allKnown(ArrayList<String> wordList)
{
    boolean result = true;
    for(int index = 0; index < wordList.size() && result; index++)
    {
        if(!words.contains(wordList.get(index)))
        {
            result = false;
        }
    }
    return result;
}

如果words确实是一个数组而不是ArrayList,那么它没有contains方法,你必须要么有一个双循环,要么转换它列表:

  List<String> parentWords = Arrays.asList(words);
  ...
  if (parentWords.contains(...))

答案 3 :(得分:0)

不要在if之后将结果重置为true。因为这样整个函数总是会返回true。

答案 4 :(得分:0)

一些提示:

  1. 不要将ArrayList用作方法参数,请始终使用更抽象的List(您的代码都不依赖于ArrayList,因此您可以稍后更改实施,如果您等)。
  2. 使用下面显示的简化语法迭代List个对象。
  3. 您只需要一个单词不在words列表中即可返回false,所以就这样做(如下所示)。
  4. public boolean allKnown(List<String> wordList) {
        for (String word : wordList) {
            if (!words.contains(word)) {
                return false;
            }
        }
        return true;
    }
    

答案 5 :(得分:0)

public boolean allKnown(ArrayList<String> wordList)
{
    boolean result = true;
    for(String word : wordList)
    {
        if(!words.contains(word))
        {
            result = false;
        }
    }
    return result;
}

答案 6 :(得分:0)

这是一个更简单的版本:

public boolean allKnown(List<String> wordList) {
   List<String> wordListCopy = new ArrayList<String>(wordList);
   return !wordListCopy.retainAll(words);
}

PS: retainAll()wordList中删除了dictionnary中未包含的所有元素wordList。如果您的wordList因调用而更改(删除非现有元素后),则此方法返回 true ,换句话说,此方法在您dictionnary时返回false您{{1}}中存在元素。

相关问题