public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
private transient Entry<E> header = new Entry<E>(null, null, null); //队列的头部
public LinkedList() { //默认构造函数 空的队列 只有一个头!
header.next = header.previous = header;
}
LinkedList 使用 private static class Entry<E> {
E element;
Entry<E> next;
Entry<E> previous; 存储元素,双向循环链表
private Entry<E> entry(int index) { 这个函数将给定的位置值 找到具体对应的Entry 为了提高效率 size>>1 返回 整 个队列的中间位置 然后根据index的相对位置来决定从前还是从后找
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("Index: "+index+
", Size: "+size);
Entry<E> e = header;
if (index < (size >> 1)) {
for (int i = 0; i <= index; i++)
e = e.next;
} else {
for (int i = size; i > index; i--)
e = e.previous;
}
return e;
}
此外 ,LinkedList里面还有两个private class 来充当 Iterator 迭代器
private class DescendingIterator implements Iterator 反向的!
private class ListItr implements ListIterator<E>
JDK源码学习- LinkedList
最新推荐文章于 2024-08-29 22:44:56 发布
本文详细介绍了Java中LinkedList类的内部实现原理,包括双向循环链表的存储方式、头节点的作用以及通过索引查找节点的具体算法。此外还介绍了LinkedList中用于迭代的内部类。
223

被折叠的 条评论
为什么被折叠?



