我有一個名爲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>
它抱怨這裏的附加聲明?
什麼_specifically_給你悲傷? – mre
的compareto部分,即時通訊不能確定我如何去獲取單詞列表並將它們添加到suggestListOfWords列表 – EI756
首先,'suggestedListOfWords'應該是一個實際的列表,不是null。其次,你不想只在相同的情況下添加這個單詞,但如果一個可能的單詞*開始*已經輸入的內容(我會假設)。第三,爲什麼不像「for(String w:wordsList)'遍歷wordsList?而且,一個*特里*會更有效率,但是...讓我們首先得到*東西*。 –