分而治之 :)
而不是存儲所有字典數據成單個列表....對於每個字符像創建數組列表,B,C,d(你將有總26清單:一個是每個字母)
Map<Character, List<String>> map = new HashMap<Character, List<String>>();
// creating list for each char
for(int i=0;i<26;i++){
char ch = (char) ('a' + i);
map.put(ch,new ArrayList<String>());
}
// storing some sample dictionary data or make a function for it
map.get("abcd".charAt(0)).add("abcd");
map.get("bcde".charAt(0)).add("bcde");
map.get("cdef".charAt(0)).add("cdef");
map.get("fghi".charAt(0)).add("fghi");
map.get("ijkl".charAt(0)).add("ijkl");
String searchWord = "cd";
// searh the given String
List<String> wordList =map.get(searchWord.charAt(0));
int idx =0;
for (String st : wordList) {
if (st.startsWith(searchWord)) {
idx = wordList.indexOf(st);
System.out.println("position="+idx); //display index in log
break;
}
}
// if require, In idx variable : add the size() of all list
// which come before the give searh char
// ListView.setSelection(idx); //set listView scroll
}
注意:請轉換在搜索或存儲之前將大寫字母縮寫爲小寫字母。
請參閱此鏈接,以及 的http://計算器.com/questions/2000237/in-java-which-most-recommended-class-class-for-a-dictionary-data-structure –
哦,這真的很有幫助謝謝 – user2637015