如果两个字符串匹配,则返回建议单词列表

时间:2011-10-05 12:23:41

标签: java autocomplete

我有一个名为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> 它在这里抱怨add语句?

2 个答案:

答案 0 :(得分:1)

对于这类事情,Trie是一个很好的数据结构。

答案 1 :(得分:0)

的问题
            phrases.add(s);

phrasesList < Interface >sString

phrases持有Interfaces,您正试图在其中添加String