简体   繁体   English

根据另一个数组列表检查一个数组列表

[英]Checking an array list against another array list

This is a method in a spell checker. 这是拼写检查器中的一种方法。 As the header explains, it should return true if and only if all the words added to the arraylist are found in the parent array, words. 正如标题所解释的,仅当在父数组中找到所有添加到arraylist的单词时,才应返回true。 Otherwise it should return a false value. 否则,它将返回一个假值。 I've been fighting with this for a few hours and this is my current situation... 我已经为此战斗了几个小时,这就是我目前的情况...

    /**
    * 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;
    }

All I really need is a way to turn out a yes or no, but I'm lost. 我真正需要的只是一种证明是或否的方法,但我迷路了。 Please try and work with the code given as this is an exercise to teach that code. 请尝试使用给出的代码,因为这是教该代码的练习。 Thanks! 谢谢!

Your problem is here: 您的问题在这里:

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

!wordList.contains(index) is a boolean expression, so it always evaluates to either true or false . !wordList.contains(index)是一个布尔表达式,因此它总是计算为truefalse So you're actually checking if the words list contains true or false, not the word like you want. 因此,您实际上是在检查words列表中包含的是真还是假,而不是您想要的单词。 Replace it with if(!words.contains(wordList.get(index)) to check if the current word is found in the dictionary. if(!words.contains(wordList.get(index))替换它,以检查是否在词典中找到了当前单词。

I would suggest a following solution: iterate wordList word by word, and for each word check if it's found in the dictionary. 重复:我建议一个解决方案如下wordList ,如果它在字典中的一字一句,并为每个字检查。 If not so, return false immediately. 如果不是这样,请立即返回false。 If you reach the end of the loop, return true. 如果到达循环末尾,则返回true。

Here could be another solution: 这可能是另一种解决方案:

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

For example: 例如:

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));

Prints: 打印:

true
false

Take out result = true; 取出result = true; - you don't want to reset the value to true at every step in the loop. -您不想在循环的每一步都将值重置为true

Also change wordList.contains to wordList.get (because you want to get the word at a specific index, not check if it's contained in wordList ) and move the ! 还要将wordList.contains更改为wordList.get (因为要在特定索引处获取单词,而不是检查单词是否包含在wordList ),然后移动! out (because you can't 'not' a string). (因为您不能“不”字符串)。

And you can also optimize by checking result 's value in the for-loop condition (or simply returning directly in the if-statement). 而且,您还可以通过在for循环条件下检查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;
}

If words really is an array and not an ArrayList , it doesn't have a contains method, you'll have to either have a double for-loop, or convert it to a list: 如果words确实是一个数组而不是ArrayList ,则它没有contains方法,则必须有一个double for循环,或将其转换为列表:

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

Don't reset result to true after your if. 如果没有,请勿将结果重置为true。 Because like this the whole function will always return true. 因为这样,整个函数将始终返回true。

A few tips: 一些提示:

  1. Don't use ArrayList as a method parameter, always use the more abstract List (none of your code depends on ArrayList , so you can change the implementation later, if you like). 不要将ArrayList用作方法参数,请始终使用更抽象的List (您的代码都不依赖ArrayList ,因此您可以根据需要稍后更改实现)。
  2. Iterate over List objects using the simplified syntax shown below. 使用下面显示的简化语法遍历List对象。
  3. You only need one word to be not in the words list to return false , so do exactly that (as shown below). 您只需要一个单词不在words列表中即可返回false ,因此可以完全做到这一点(如下所示)。

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

Here is a simpler version : 这是一个简单的版本:

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

PS : retainAll() removes from you wordList all of its elements that are not contained in you dictionnary . PS: retainAll()从您的wordList删除了dictionnary中未包含的所有元素。 This method return true if your wordList changed as a result of the call (after removing the non existing element), in other word, this method return false when all your wordList elements exists in you dictionnary . 该方法返回true,如果你的wordList改变为调用的结果(除去非现有元素之后),换句话说,该方法返回false当你所有的wordList元素存在于你dictionnary

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM