2

这是拼写检查器中的一种方法。正如标题所解释的,当且仅当所有添加到 arraylist 的单词都在父数组 words 中找到时,它才应该返回 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;
    }

我真正需要的只是一种判断是或否的方法,但我迷路了。请尝试使用给出的代码,因为这是教授该代码的练习。谢谢!

4

7 回答 7

2

你的问题在这里:

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

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

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

于 2013-10-24T07:49:16.123 回答
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
于 2013-10-24T08:00:55.087 回答
1

取出result = true;- 您不想true在循环中的每一步都将值重置为。

还要更改wordList.containswordList.get(因为您想在特定索引处获取单词,而不是检查它是否包含在 中wordList)并!移出(因为您不能“不是”字符串)。

您还可以通过检查resultfor 循环条件中的值(或直接在 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方法,你必须要么有一个双 for 循环,要么将它转换为一个列表:

  List<String> parentWords = Arrays.asList(words);
  ...
  if (parentWords.contains(...))
于 2013-10-24T07:47:07.173 回答
0

不要在 if 之后将结果重置为 true。因为像这样整个函数将始终返回 true。

于 2013-10-24T07:47:22.040 回答
0

一些提示:

  1. 不要ArrayList用作方法参数,始终使用更抽象List的(您的代码都不依赖于ArrayList,因此您可以稍后更改实现,如果您愿意)。
  2. List使用如下所示的简化语法迭代对象。
  3. 您只需要一个单词不在words列表中即可 return false,所以就这样做(如下所示)。

public boolean allKnown(List<String> wordList) {
    for (String word : wordList) {
        if (!words.contains(word)) {
            return false;
        }
    }
    return true;
}
于 2013-10-24T07:53:46.927 回答
0
public boolean allKnown(ArrayList<String> wordList)
{
    boolean result = true;
    for(String word : wordList)
    {
        if(!words.contains(word))
        {
            result = false;
        }
    }
    return result;
}
于 2013-10-24T07:55:50.350 回答
0

这是一个更简单的版本:

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

PS: retainAll()从你身上删除wordList所有不包含在你身上的元素dictionnary。如果您因调用而更改(在删除不存在的元素之后),则此方法返回truewordList,换句话说,当您的所有wordList元素都存在于您中时,此方法返回 false dictionnary

于 2013-10-24T08:20:03.853 回答