如何制作listObject的副本?

listObject的类:

static class listObject {
        ArrayList<String> diseases;
        ArrayList<String> images;

        static class guide {
            ArrayList<String> guideTitle;
            ArrayList<String> guideText;
            ArrayList<String> guideImage;
        }
    }


这是我的复制功能:

public static Object copy(Object orig) {
        Object obj = null;
        try {
            // Write the object out to a byte array
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(bos);
            out.writeObject(orig);
            out.flush();
            out.close();

            // Make an input stream from the byte array and read
            // a copy of the object back in.
            ObjectInputStream in = new ObjectInputStream(
                    new ByteArrayInputStream(bos.toByteArray()));
            obj = in.readObject();
        }
        catch(IOException e) {
            e.printStackTrace();
        }
        catch(ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
        }
        return obj;
    }


当我调用copy()函数时,它返回null:

listObject list;
listObject _tempList = new listObject();
list = (listObject) copy( (Object) _tempList);

最佳答案

listObject应该实现Serializable。如下更改listObject。

class listObject implements Serializable{
ArrayList<String> diseases;
ArrayList<String> images;

static class guide {
    ArrayList<String> guideTitle;
    ArrayList<String> guideText;
    ArrayList<String> guideImage;
}
}

10-05 17:56