简体   繁体   English

存储/读取大量字符串的好方法?

[英]A good way to store/read a large amount of strings?

I'm developing a "funny quotes" app for android. 我正在为android开发一个“有趣的引号”应用程序。 I have over 1000 quotes which I want to use inside my app but I don't know whether I should use a database or text file. 我有超过1000个引号,我想在我的应用程序中使用,但我不知道我是否应该使用数据库或文本文件。 Please note that the app should not read the same sentence twice. 请注意,应用程序不应该两次读同一个句子。 and it has a previous/next button so i need to keep track of the previous quotes. 它有一个上一个/下一个按钮,所以我需要跟踪以前的报价。 please tell me which one is better and more optimized. 请告诉我哪一个更好,更优化。 Also, if you can please link me to a good tutorial about storing/reading the data. 另外,如果可以,请将我链接到一个关于存储/读取数据的好教程。

thanks 谢谢

Use a database. 使用数据库。 It's faster and more flexible than a text file. 它比文本文件更快,更灵活。 One day you will extend the app and then you will be glad you used a database. 有一天你会扩展应用程序,然后你会很高兴你使用了数据库。 I recommend to, when you boot up the app, just select all the rows using the in-built random functionality of your database. 我建议,当您启动应用程序时,只需使用数据库的内置随机功能选择所有行。 1000 rows should not take too long. 1000行不应该花太长时间。 Then just iterate through the resulting ArrayList (or whatever you choose to use) of strings you end up with - the first quote you show will be element 0 from that list, the second element element 1 from that list, and so on. 然后只需遍历最终得到的字符串列表(或您选择使用的任何字符串) - 您显示的第一个引用将是该列表中的元素0,该列表中的第二个元素元素1,依此类推。 If you use this approach, you won't need any other structure to keep track of used quotes - just use the iterator variable that you use for indexing the quote array. 如果使用此方法,则不需要任何其他结构来跟踪使用的引号 - 只需使用用于索引引用数组的迭代器变量。

fetchAllRows on this page seems to be what you want for getting the data. 此页面上的fetchAllRows似乎是您获取数据所需的内容。

If you choose not to keep too much in memory, you could keep just a list of quote IDs that have been used so far. 如果您选择不在内存中保留太多内容,则可以只保留到目前为止已使用的引用ID列表。 The last element of that list would be the current quote, and the previous elements would be what the user should see when they press the back button. 该列表的最后一个元素是当前引用,前面的元素将是用户按下后退按钮时应该看到的内容。

If you will never read the same string twice the I will recommend you to not use String class as there objects are immutable and will stick in the string pool waiting to be reassigned to a reference, but that will never happen as you will never read the same string twice. 如果你永远不会读两次相同的字符串,我会建议你不要使用String类,因为对象是不可变的,并且会粘在字符串池中等待重新分配给引用,但是这永远不会发生,因为你永远不会读取相同的字符串两次

The use of DB's will over complicate things. DB的使用会使事情复杂化。

I suggest you to read a flat file in bytes and then translate them to StringBuider objects hence keeping it simple enough but still preventing intensive GC(). 我建议你以字节为单位读取一个平面文件,然后将它们转换为StringBuider对象,从而保持它足够简单但仍然阻止密集的GC()。

I hope it helps.. 我希望它有帮助..

USing DB should be fine as I think you would not want all the data in memory. 使用DB应该没问题,因为我认为你不希望内存中的所有数据。 You can keep all the quotes in DB and keep a flag to keep track whether a quote was read or not (simply update it to true once read.) 您可以在DB中保留所有引号并保留一个标记以跟踪是否读取了引用(只需读取一次就将其更新为true。)

This way you can choose from any of the quote which has the flag as false. 这样,您可以选择任何标志为false的引用。

Have you considered CsvJdbc ? 你考虑过CsvJdbc吗? You have the benefit of simple csv files with an easy upgrade path to a real database later when you have a significant number of records. 当您拥有大量记录时,您可以获得简单的csv文件,并且可以轻松升级到真实数据库。

1k records is quite small and in my opinion not sufficient to merit a database. 1k记录非常小,在我看来不足以值得数据库。

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

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