Home > Mobile >  How to store over 300 words with definitions in a Android studio
How to store over 300 words with definitions in a Android studio

Time:01-14

I'm trying to create an android app where you can learn hard words its over 300 words. I'm wondering how I should store the data in java.

I have a text file where all the words are. I have split the text so I have one array with the words and another Array with the definitions, they have the same index. In an activity, I want to make it as clean as possible, because sometimes I need to delete an index and It's not efficient to that with an ArrayList since they all need to move down. PS. I really don't wanna use a database like Firebase.

CodePudding user response:

Instead of using two different arrays and trying to ensure that their order/indices are matched, you should consider defining your own class.

class Word {

     String wordName;
     String wordDefinition;

}

You can then make a collection of this using ArrayList or similar.

ArrayList<Word> wordList;

I know you were concerned about using an ArrayList due to the large number of words, however I think for your use case the ArrayList is fine. Using a database is probably overkill, unless if you want to put in the whole dictionary ;)

In any case, it is better to define your own class and use this as a "wildcard" to collection types which accept these. This link may give you some ideas of other feasible data types.

https://developer.android.com/reference/java/util/Collections

CodePudding user response:

I personally would use a HashMap.

The reason for this is because you can set the key to be the word and the value to be the definition of the word. And then you can grab the definition of the word by doing something like

// Returns the definition of word or null if the word isn't in the hashmap   
hashMapOfWords.getOrDefault(word, null);

Check out this link for more details on a HashMap https://developer.android.com/reference/java/util/HashMap

  • Related