1

How can I search through an String-Array? I've got an dictionary app and the words are saved in a String-Array and it would be user-friendlier, if you could search for the word you want to look up, instead of looking its way to the word. Can somebody help?

Thanks.

Ahmad
  • 69,608
  • 17
  • 111
  • 137

3 Answers3

0

Not entirely sure what you are looking for- do you want to see if the word is in the array, or what is the goal? If you want the user to get to the word faster, changing the string-array will not help the UI jump to the right place.

If you want it to be like Google suggest you could take your array and make a tree data object, which each node representing a letter in a word. Then if the user types a, you go into that node and offer possible words.

AJcodez
  • 31,780
  • 20
  • 84
  • 118
  • Well you will need an EditText for user entry, and an OnTextChangeListener (or something like that) and a Tree data structure http://stackoverflow.com/questions/3522454/java-tree-data-structure – AJcodez Mar 14 '12 at 08:05
0

You may use the Arrays.binarySearch() method to search an element from the sorted array.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
0

You can try using an ArrayList instead. Then you can see if the word is in your 'dictionary' by using the contains method, ex:

ArrayList <String> myDictionary = new ArrayList<String>();
myDictionary.add(new String("foo"));
myDictionary.add(new String("bar"));


...
// To check if the word exists in your dictionary.
if (myDictionary.contains(new String("word_to_look_up")))
{
}
triad
  • 20,407
  • 13
  • 45
  • 50