分而治之 :)
而不是存储所有字典数据成单个列表....对于每个字符像创建数组列表,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