JDK源码学习- LinkedList

本文详细介绍了Java中LinkedList类的内部实现原理,包括双向循环链表的存储方式、头节点的作用以及通过索引查找节点的具体算法。此外还介绍了LinkedList中用于迭代的内部类。
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>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值