如何序列化在java中实现的链表? [英] How to serialize a linked list implemented in java?
本文介绍了如何序列化在java中实现的链表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在网上读到,通过将派生对象声明为瞬态,可以省略序列化。但是,在链表的情况下,链接是对象之间的内存引用。那么,我应该将它转换为数组并存储数组表示吗?
I read online that serialization can be omitted for derived 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?
推荐答案
以下是Java序列化的方法 LinkedList
:它获取所有元素并将它们与大小一起写入 ObjectOutputStream
。当然,声明标题
条目瞬态
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
参见 writeObject
和 readObject
的方法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);
这篇关于如何序列化在java中实现的链表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文