2

オブジェクトの各側面を調べて保存することなく、オブジェクトの配列を保存する簡単な方法があるかどうか疑問に思っていました。この例では、1 つの配列と 2D 配列の 2 つの配列があり、カスタム クラスを参照するオブジェクトが含まれています。各オブジェクトには、x および y の int、ブール値、文字列などの特定の詳細があります。それら (block[0].x、block[0].canWalk、block[0].name) に添付されており、for ループを使用せずにこれらの配列をファイルに保存する簡単な方法があるかどうか疑問に思っていました。各パーツを保存します。多次元配列は、最初の配列と同じ保存された配列の配列です (savedBlock[0][0].x ...)

私がこれまでに持っているもの(NotSerializableExceptionを投げる):

public class Save
{
    static File f;
    static ObjectOutputStream os;
    public static void openFile()
    {
        try
        {
            if(!new File("c:\\IDsGame").exists())
            {
                new File("c:\\IDsGame").mkdirs();
            }

            f = new File("c:\\IDsGame\\data.bin");
            os = new ObjectOutputStream(new FileOutputStream(f));

            writeFile();
        }
        catch(Exception e)
        {
            System.err.println("creating file");
        }
    }

    public static void writeFile()
    {
        try
        {
            ArrayList<Object> map = new ArrayList<Object>(Arrays.asList(Map.block));
            ArrayList<Object> savedMaps = new ArrayList<Object>(Arrays.asList(Map.savedMaps));
            os.writeObject(map);
            os.writeObject(savedMaps);
            os.close();
        }
        catch (IOException e) {System.out.println(e);}

    }
}

マップ クラス内で、ブロック (Blocks[]) と savedMaps(Blocks[][]) を初期化します。私の Blocks クラスはこれを保持しています:

public class Blocks implements Serializable
{
    public boolean canWalk, onTop, itemTaken;
    public Image img = null, imgBack = null;
    public final Image (a ton of different images)
    public String name, item, message, title;
    public char initMap, initEx, initIt;
    public int x, y, height, width;

    public Blocks()
    {
        canWalk = true;
        onTop = false;
        itemTaken = false;
        img = null;
        name = null;
        item = null;
        message = null;
        x = 0;
        y = 0;
        height = 0;
        width = 0;
    }
}

明らかに、私は特定の部分を Map クラス内の異なる配列に変更しました。また、Blocks オブジェクトの配列を保存するためのより簡単な方法 (まったく) があるかどうか疑問に思っていました。

ご協力いただきありがとうございます。さらに具体的な情報が必要な場合はお知らせください。

ID

4

2 に答える 2

0

Just making a class implement Serializable is not enough: All the fields must be Serializable too.

Your Block class may have a problem. All the usual java classes are Serializable, but Block also has fields of type Image. If Image isn't Serializable, then attempting to serialize Block will throw NotSerializableException.

于 2012-06-10T06:18:55.800 に答える
0

Image はシリアル化できないため、Blocks クラスがシリアル化されると NotSerializableException を受け取ります。ImageIcon はシリアル化できるため、Image インスタンスを ImageIcons でラップすると、その問題が解決します。

public class Blocks implements Serializable
{
    public boolean canWalk, onTop, itemTaken;
    public ImageIcon img = null, imgBack = null;
    public final ImageIcon (a ton of different images)
    public String name, item, message, title;
    public char initMap, initEx, initIt;
    public int x, y, height, width;

    public Blocks()
    {
        canWalk = true;
        onTop = false;
        itemTaken = false;
        img = null;
        // img = new ImageIcon(someImageInstance)
        name = null;
        item = null;
        message = null;
        x = 0;
        y = 0;
        height = 0;
        width = 0;
    }
}
于 2012-06-10T05:49:06.607 に答える