2011-10-05 42 views
0

我有一個名爲ItemList的類,它用於提供自動完成文本字段的建議字詞列表。因此,當用戶輸入一個字母時,會出現一個下拉菜單,其中包含建議詞彙列表。如果兩個字符串匹配,則返回建議字詞列表

我無法使用此功能所需的代碼。

public List<Interface> SuggestedListOfWords(String prefix) { 


     int i = 0; 
     List<Interface> suggestedListOfWords = null; 

     while(i != wordsList.size()) { 

      String wordElement = wordsList.elementAt(i); 
      Item tempItem = new Item(wordElement); 
      //String item = wordsList.elementAt(i); 
      String itemName = tempItem.name; 
      int compareResult = itemName.compareTo(prefix); 

      if(compareResult == 0) { 



      } 

      i++; 
     } 

     return suggestedListOfWords; 
    } 

有什麼建議嗎?

編輯:

for (String s : wordsList) { 
      if (s.startsWith(prefix)) 
       phrases.add(s); 
     } 

詞的類型爲List<Interface> 它抱怨這裏的附加聲明?

+3

什麼_specifically_給你悲傷? – mre

+0

的compareto部分,即時通訊不能確定我如何去獲取單詞列表並將它們添加到suggestListOfWords列表 – EI756

+0

首先,'suggestedListOfWords'應該是一個實際的列表,不是null。其次,你不想只在相同的情況下添加這個單詞,但如果一個可能的單詞*開始*已經輸入的內容(我會假設)。第三,爲什麼不像「for(String w:wordsList)'遍歷wordsList?而且,一個*特里*會更有效率,但是...讓我們首先得到*東西*。 –

回答

0

  phrases.add(s); 

的問題是,phrasesList <Interface>, 和sString

phrases持有Interfaces,並且您正試圖將String放入其中。

+0

那麼只需要一個字符串類型的列表會更好嗎? – EI756

+0

@ EI756 - 是的,它至少會編譯!您需要更改方法簽名以返回列表。 –

+0

對,但不可能有一個類型界面的列表,並安裝列表像列表 list1 = new List (); ? – EI756

1

A Trie對於這類事情來說是一個很好的數據結構。

相關問題