LinkedList源码浅析 (三)

2014-11-24 02:43:04 · 作者: · 浏览: 2
throws java.io.IOException, ClassNotFoundException {
// Read in any hidden serialization magic
s.defaultReadObject();

// Read in size
int size = s.readInt();

// Initialize header
header = new Entry(null, null, null);
header.next = header.previous = header;

// Read in all elements in the proper order.
for (int i=0; i addBefore((E)s.readObject(), header);
}
/**
* Save the state of this LinkedList instance to a stream (that
* is, serialize it).
*
* @serialData The size of the list (the number of elements it
* contains) is emitted (int), followed by all of its
* elements (each an Object) in the proper order.
*/
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
// 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);
}

/**
* Reconstitute this LinkedList instance from a stream (that is
* deserialize it).
*/
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in any hidden serialization magic
s.defaultReadObject();

// Read in size
int size = s.readInt();

// Initialize header
header = new Entry(null, null, null);
header.next = header.previous = header;

// Read in all elements in the proper order.
for (int i=0; i addBefore((E)s.readObject(), header);
}

综上 ,我们可以看出。LinkedList是一个双向链表,它可以被当成栈,队列或双端队列来使用。它也提供 了随机访问元素的方法,不过这个访问的时间复杂度并不像ArrayList是O(1),而是O(n)。它删除给定位置的元素

的效率其实并不比ArrayList高。但它删除特定元素的效率 要比ArrayList高。

作者“KevinJom的专栏”