简体   繁体   English

Android:保存 int 列表的最有效方法

[英]Android: Most efficient way to save a list of int

I'm trying to create a system to save the list of levels that have already been played in my application.我正在尝试创建一个系统来保存已在我的应用程序中播放的关卡列表。

I've been using Editor and SharedPreferences to store information on the phone.我一直在使用 Editor 和 SharedPreferences 在手机上存储信息。

But now I have a list of int to save但现在我有一个要保存的 int 列表

I've been trying to figure out something with我一直在想办法

 DataOutputStream out;

        //Size
        int resultSize = results.size();
        out.writeInt(resultSize);

        //All the levels ID
        for (int idx = 0; idx < resultSize; idx++){
            //l().debug("Lvl ID: "+results.get(idx));
            out.writeInt(results.get(idx));
        }

But then I don't know how to store this in the shared preferences.但是我不知道如何将其存储在共享首选项中。

I'm also concerned about size, this list of ID might get really big so I want to optimize the amount stored.(hence the DataStream)我也担心大小,这个 ID 列表可能会变得非常大,所以我想优化存储的数量。(因此 DataStream)

any ideas有任何想法吗

Jason杰森

You could iterate through your list and construct a comma delimitted String from the ints.您可以遍历您的列表并从整数构造一个逗号分隔的字符串。 Eg "1,2,15..etc", then just write this string to SharedPreferences.例如“1,2,15..etc”,然后将这个字符串写入 SharedPreferences。 You would obviously have to tokenize it when you read it back.当您读回它时,您显然必须对其进行标记。

How big is "really big" exactly? “真的很大”到底有多大? Considering it's a list of levels played by a human I am going to assert that it is "small" for what a smartphone can handle.考虑到它是一个人类玩过的关卡列表,我将断言它对于智能手机可以处理的东西来说是“小”的。 Though, personally I would recommend this to go in a SQLite table of "completed" levels for ease of querying the information.不过,我个人会在“已完成”级别的 SQLite 表中向 go 推荐这个,以便于查询信息。

I don't think that you need the most efficient way.我认为您不需要最有效的方法。 This data is small, go with what you know.这个数据很小,go 有你知道的。 Pick something simple and understandable, something easy to debug.选择简单易懂、易于调试的东西。

A flat file with every level int on a new line.一个平面文件,每个级别 int 在一个新行上。 You can't get much simpler than that.没有比这更简单的了。 Sure, it's bigger than the most efficient way, but it's much easier to recover from corrupted files, much easier to debug.当然,它比最有效的方法更大,但是从损坏的文件中恢复要容易得多,调试起来也容易得多。

Binary stream is the most efficient way how to save integers.二进制 stream 是保存整数的最有效方法。 If you have really a lot of numbers (megabytes), use DataOutputStream combined with ZipOutputStream.如果您确实有很多数字(兆字节),请结合使用 DataOutputStream 和 ZipOutputStream。

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

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