1

I use ArrayList for my Dictionary Index Data

I want to make similar search system

for example

dictionary data : 'abcd' 'bcde' 'cdef' 'fghi' 'ijkl'

if i search 'cd' i want to get index '3'

in my source

for (String st : wordList) {
    if (st.indexOf(searchWord) == 0) {
        ListView.setSelection(index); //set listView scroll
        break;
    }
    index++;
}

but it took too much time :(

what is the best way to make this system?

Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
user2637015
  • 723
  • 2
  • 12
  • 27

2 Answers2

1

just remove index++ from loop and change if(CONDITION) like this.

  for (String st : wordList) {
    if (st.startsWith(searchWord)) {
       System.out.println("position="+wordlist.indexOf(st));//display index in log
       ListView.setSelection(wordlist.indexOf(st)); //set listView scroll
       break;
      }
  }
Paweł Piecyk
  • 2,749
  • 15
  • 17
Ketan Ahir
  • 6,678
  • 1
  • 23
  • 45
0

Divide and Rule :)

Rather than storing all dictionary data into single list .... create array list for each Char like a,b,c,d (you will have total 26 list : one is for each alphabet)

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

}

Note : please convert the upper case word to lower case before searching or storing.

Rakesh Soni
  • 10,135
  • 5
  • 44
  • 51
  • See this link as well http://stackoverflow.com/questions/2000237/in-java-which-is-the-most-recommended-class-for-a-dictionary-data-structure – Rakesh Soni Aug 17 '13 at 11:00