简体   繁体   English

如何在 Android 工作室中存储超过 300 个带定义的单词

[英]How to store over 300 words with definitions in a Android studio

I'm trying to create an android app where you can learn hard words its over 300 words.我正在尝试创建一个 android 应用程序,您可以在其中学习超过 300 个单词的硬单词。 I'm wondering how I should store the data in java.我想知道我应该如何将数据存储在 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.在一个活动中,我想让它尽可能干净,因为有时我需要删除一个索引并且使用 ArrayList 效率不高,因为它们都需要向下移动。 PS.附言。 I really don't wanna use a database like Firebase.我真的不想使用像 Firebase 这样的数据库。

Instead of using two different arrays and trying to ensure that their order/indices are matched, you should consider defining your own class.与其使用两个不同的 arrays 并尝试确保它们的顺序/索引匹配,不如考虑定义自己的 class。

class Word {

     String wordName;
     String wordDefinition;

}

You can then make a collection of this using ArrayList or similar.然后,您可以使用 ArrayList 或类似方式收集此信息。

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.我知道您担心使用 ArrayList 由于单词过多,但我认为对于您的用例 ArrayList 没问题。 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.在任何情况下,最好定义您自己的 class 并将其用作接受这些的集合类型的“通配符”。 This link may give you some ideas of other feasible data types.此链接可能会给您一些其他可行数据类型的想法。

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

I personally would use a HashMap .我个人会使用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查看此链接以获取有关HashMap https://developer.android.com/reference/java/util/HashMap的更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM