开发者

Return a list of suggested words if two strings match

开发者 https://www.devze.com 2023-04-11 10:53 出处:网络
I have class called ItemList, which is used to provide a list of suggested words for an auto-complete textfield. So as the user types a letter, a dropdown menu appears with a list of suggested words.

I have class called ItemList, which is used to provide a list of suggested words for an auto-complete textfield. So as the user types a letter, a dropdown menu appears with a list of suggested words.

I'm having trouble with the code required for this functionality.

public List<Interface> SuggestedListOfWords(String prefix) {


        int i = 0;
        List<Interface> suggestedListOfWords 开发者_JS百科= 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;
    } 

Any suggestions?

EDIT:

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

Phrases is of type List<Interface> It's complaining about the add statement here?


A Trie is a good data structure for this sort of thing.


The problem with

            phrases.add(s);

is that phrases is a List < Interface > , and s is a String.

phrases holds Interfaces, and you're trying to put a String in it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号