2013-10-24 125 views
2

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

我真正需要的是转出yes或no的方式,但我迷路了。 请尝试使用给出的代码,因为这是一个练习来教授代码。 谢谢!

+0

复制正在测试的ArrayList,然后copy.removeAll(已知)并测试复制的大小,如果0中的所有内容都已知。 –

回答

1

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

也改变wordList.containswordList.get(因为你想在一个特定的指数获得了这个词,如果它包含在wordList不检查),移动!出来(因为你不能“不”的字符串)。

而且您还可以通过在for-loop条件中检查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(...)) 
0

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

2

你的问题是在这里:

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

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

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

0

一些提示:

  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; 
} 
0
public boolean allKnown(ArrayList<String> wordList) 
{ 
    boolean result = true; 
    for(String word : wordList) 
    { 
     if(!words.contains(word)) 
     { 
      result = false; 
     } 
    } 
    return result; 
} 
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 
0

下面是一个简单的版本:

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

PS:retainAll()从你wordList的所有元素中删除未包含在你dictionnary。此方法返回true如果您的wordList因呼叫而改变(在移除不存在的元素后),换句话说,当您的所有元素存在于dictionnary中时,此方法返回false。