简体   繁体   English

程序(在.java中)的所有字符串是否在启动时存储在RAM中(Android)?

[英]Are all the strings from a program (in .java) stored in RAM when it's launched (Android)?

I'm writing a game on Android similar to Who wants to be a millionaire, and I'm wondering what s the best way to store the questions and answers. 我在Android上写的游戏类似于谁想要成为百万富翁,我想知道存储问题和答案的最佳方式是什么。 I want to have at least a 300-400 questions to start with and add about 100 every update. 我希望至少有300-400个问题,并且每次更新都会增加约100个问题。 Right now I have them stored in a static class like this: 现在我将它们存储在这样的静态类中:

public class Qs {
public static Question questions[][];
public static Question q1e1,q1e2,q1e3,q2e1,q2e2,q2e3,q3e1,q3e2,q3e3,q4e1,q4e2,q4e3;

public static void load(){



    q1e1 =  new Question("Question","Answer","wrong answer","wrong answer","wrong answer");
    q1e2 =  new Question("Question","Answer","wrong answer","wrong answer","wrong answer");
    q1e3 =  new Question("Question","Answer","wrong answer","wrong answer","wrong answer");


    Question level1[] = new Question[]{q1e1,q1e2,q1e3};
    Question level2[] = new Question[]{q2e1,q2e2,q2e3};
    Question level3[] = new Question[]{q3e1,q3e2,q3e3};
    Question level4[] = new Question[]{q4e1,q4e2,q4e3};

    questions = new Question[][]{level1,level2,level3,level4};

}

} }

But I really don't know what will this do when I have hundreds of questions in that clas. 但是我真的不知道当我在这些文章中有数百个问题时会做什么。 Will they all load into memory and probably slow the phone down a lot ? 它们都会加载到内存中并且可能会使手机放慢速度吗? For editing and adding questions it's fine, no need for fancy xml or anything, I'm just concerned about slowing down the phone (otherwise my app is really not demanding). 编辑和添加问题很好,不需要花哨的xml或任何东西,我只关心放慢手机速度(否则我的应用程序真的没有要求)。 Thanks for any answers, comments and tips! 感谢您的任何答案,评论和提示!

Use classes for logic, not storage. 使用类来表示逻辑,而不是存储。 You can either use SharedPreferences (doesn't fit well) or SQLite (It is meant for such usage). 您可以使用SharedPreferences (不适合)或SQLite (它用于此类用法)。

您可以使用Sqlite作为持久性存储,并在需要时在内存中占用一小部分。

You should consider using an external storage for this. 您应该考虑使用外部存储。

This will make you application smaller. 这将使您的应用程序更小。 This will also ease the maintenance and allow you maintaining the questionnaire outside the app: a new version of the questionnaire will not lead you to recompile and republish the app (you can make your app to download new versions of the questionnaire from time to time, but this is another topic). 这也将简化维护并允许您在应用程序之外维护问卷:问卷的新版本将不会引导您重新编译和重新发布应用程序(您可以使您的应用程序不时下载新版本的问卷,但这是另一个话题)。

At first glance I see two valid options: 乍一看,我看到两个有效选项:

  • SQLite (lightweight database embedded in Android) SQLite(Android中嵌入的轻量级数据库)

  • Android external storage (file system that allows you storing XMLs, or keys/values text files, or any other format) Android外部存储(允许您存储XML,键/值文本文件或任何其他格式的文件系统)

There is an interesting post about that on the Android blog: http://developer.android.com/guide/topics/data/data-storage.html 在Android博客上有一篇有趣的帖子: http//developer.android.com/guide/topics/data/data-storage.html

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

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