2013-10-24 200 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。