简体   繁体   中英

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. 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 . So you're actually checking if the words list contains true or false, not the word like you want. Replace it with if(!words.contains(wordList.get(index)) to check if the current word is found in the dictionary.

I would suggest a following solution: iterate wordList word by word, and for each word check if it's found in the dictionary. If not so, return false immediately. If you reach the end of the loop, return 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; - you don't want to reset the value to true at every step in the loop.

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 ! 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).

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:

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

Don't reset result to true after your if. Because like this the whole function will always return 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).
  2. Iterate over List objects using the simplified syntax shown below.
  3. You only need one word to be not in the words list to return false , so do exactly that (as shown below).

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

retainAll() removes from you wordList all of its elements that are not contained in you dictionnary . retainAll()从您的wordList删除了dictionnary中未包含的所有元素。 This method return 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 . 如果你的wordList改变为调用的结果(除去非现有元素之后),换句话说,该方法返回false当你所有的wordList元素存在于你dictionnary

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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