【问题标题】:Checking an array list against another array list对照另一个数组列表检查一个数组列表
【发布时间】: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


【解决方案1】:

你的问题在这里:

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

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

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

【讨论】:

    【解决方案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
    

    【讨论】:

      【解决方案3】:

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

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

      您还可以通过在 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;
      }
      

      如果words 真的是一个数组而不是ArrayList,它没有contains 方法,你必须要么有一个双for循环,要么将它转换为一个列表:

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

          一些提示:

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

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

          【讨论】:

            【解决方案6】:
            public boolean allKnown(ArrayList<String> wordList)
            {
                boolean result = true;
                for(String word : wordList)
                {
                    if(!words.contains(word))
                    {
                        result = false;
                    }
                }
                return result;
            }
            

            【讨论】:

              【解决方案7】:

              这是一个更简单的版本:

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

              PS : retainAll() 会从您的wordList 中删除所有不包含在您dictionnary 中的元素。如果您的 wordList 因调用而发生更改(在删除不存在的元素之后),此方法返回 true,换句话说,当您的所有 wordList 元素存在时,此方法返回 false dictionnary.

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2017-08-20
                • 2017-04-18
                • 1970-01-01
                • 2017-11-18
                • 1970-01-01
                • 2013-03-15
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多