How to implement incremental search on a list

前端 未结 7 2094
无人共我
无人共我 2021-02-06 04:52

I want to implement incremental search on a list of strings. Consider I have an array containing which contains the strings store,state,stamp,crawl,crow. My application has a te

7条回答
  •  青春惊慌失措
    2021-02-06 05:09

    You could just look at the newly entered letter; if the new third letter is an 'a' just throw out all elements without 'a' at position three. If the user deletes a letter you have to rescan the whole original list and bring back all priviously removed items.

    But what if the user pastes multiple letters from the clipboard, deletes multiple letters by selecting them, inserts or deletes a single or multiple letters somewhere in the middle?

    You have just to many cases to watch for. You could do the method with the newly entered letter an fall back to a complete rescan if the search text changed in a way other than adding a single letter, but even this simple method is probably not worth the effort just to avoid a few ten or hundred string comparisons. As already mentioned, a Trie or Patricia trie is the way to go if you have really large data sets or want to be really quick.

提交回复
热议问题