【发布时间】:2013-11-02 19:16:46
【问题描述】:
这是拼写检查器中的一种方法。正如标题所解释的,当且仅当所有添加到 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;
}
我真正需要的只是一种判断是或否的方法,但我迷路了。 请尝试使用给出的代码,因为这是教授该代码的练习。 谢谢!
【问题讨论】:
-
复制您正在测试的 ArrayList 然后 copy.removeAll(known) 并测试副本的大小,如果 0 副本中的所有内容都是已知的。
标签: java arrays loops for-loop boolean