简体   繁体   English

从应用程序中包含的文件加载android数组

[英]Loading android array from a file included in app

I've been searching and can't get a clue on how to do this. 我一直在搜索,但对如何执行此操作一无所知。 I'm creating a block-based game, let's say 3x3 blocks on screen, each block has a int associated to know which type it is: 我正在创建一个基于块的游戏,比方说屏幕上的3x3块,每个块都有一个关联的int来知道它是哪种类型:

int[][] blocksArray = {
    { 0, 0, 0 }
    { 0, 0, 1 }
    { 0, 0, 0 }
};

What I basically want to do is save like 50 multi-dimensional arrays like this one into a file let's say "levels.txt" just like this: 我基本上想做的是将50个这样的多维数组保存到一个文件中,就像这样说“ levels.txt”:

int[][] level1 = {
    { 0, 0, 0 }
    { 0, 0, 1 }
    { 0, 0, 0 }
};
...
int[][] level50 = {
    { 0, 0, 0 }
    { 0, 0, 0 }
    { 0, 0, 0 }
};

Is this a good way of approaching the problem? 这是解决问题的好方法吗? Are there better methods? 有更好的方法吗?

I really don't want to make a string of my arrays and save them into shared prefs, because I will need to edit a lot of level arrays manually and I want to have this format, or maybe a very similar way to do it? 我真的不想创建一个字符串数组并将其保存到共享的首选项中,因为我将需要手动编辑许多级别数组,并且我想使用这种格式,或者可能是一种非常相似的方式?

You can save your arrays in a file using java.io.ObjectOutputStream(); 您可以使用java.io.ObjectOutputStream()将数组保存到文件中。

    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("file"));
    oos.writeObject(level1);
    ...
    oos.writeObject(level50);

and then read your arrays back: 然后读回数组:

    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("file"));
    level1 = (int[]][])ois.readObject();
    ...
    level50 = (int[]][])ois.readObject();

If it is really what you want 如果真的是你想要的

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

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