开发者

How to serialize a linked list implemented in java?

I read online that serialization can be omitted for deri开发者_StackOverflow中文版ved objects by declaring them as transient. But, in case of linked list the links are memory references between objects. So, should I convert it to array and store the array representation?


Here's how Java serializes LinkedList: it fetches all elements and writes them to the ObjectOutputStream, together with the size. And of course declares the header entry transient

See the writeObject and readObject methods of LinkedList:

// Write out any hidden serialization magic
s.defaultWriteObject();

// Write out size
s.writeInt(size);

// Write out all elements in the proper order.
for (Entry e = header.next; e != header; e = e.next)
    s.writeObject(e.element);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜