开发者

How to serialize a linked list implemented in java?

开发者 https://www.devze.com 2023-02-17 05:04 出处:网络
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 object

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

精彩评论

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