문제

I am trying to send an ArrayList<MyObject>() over a Socket connection. MyObject implements Serializable, and I am using the ObjectOutputStream & ObjectInputStream at respective ends of the Socket with the corresponding methods on either end. On the one end I send:

output.writeObject(myList);

On the other end,

ArrayList<MyObject> myList = (ArrayList<MyObject>) input.readObject();

Now my question is if this is valid. I know that MyObject is serializable and there is no problem sending one of them over at a time. However, is this property preserved if I am sending multiple of these objects in some sort of Java collection?

도움이 되었습니까?

해결책 2

Serializing an array list is not a problem. Because it implements Serializeable. But you need to make sure MyObject doesn't contain reference to another class that doesn't implement serializable.

And object graph is maintained when you deserialize you will get everything back minus transient variables.

다른 팁

Yes. Object graphs are preserved in full generality, and ArrayList is Serializable.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top