LinkedList学习笔记


推荐阅读博文:

JAVA LinkedList和ArrayList的使用及性能分析

http://www.jb51.net/article/42767.htm


ArrayList是一个动态数组(快速随机访问元素),LinkedList是双向链表实现的(快速插入,删除元素)。其特性是由其数据结构决定的。

LinkedList也是一个简单的数据结构。用法也与ArrayList相似。

    public boolean add(E e) {
        linkLast(e);
        return true;
    }
linkLast(e)方法是将元素添加到链表的结尾。


/**
     * Links e as last element.
     */
    void linkLast(E e) {
        final Node<E> l = last;
        final Node<E> newNode = new Node<>(l, e, null);
        last = newNode;
        if (l == null)
            first = newNode;
        else
            l.next = newNode;
        size++;
        modCount++;
    }


LinkedList定义了first和last。用于标记头和尾。

 transient Node<E> first;

transient Node<E> last;

如果是插入的元素是第一个元素,即LinkedList没有结尾(last)元素:


如果插入的元素是第二个元素:


依次类推,不外如是。


get(int index) :返回此列表中指定位置处的元素。

       if (index < (size >> 1)) {
            Node<E> x = first;
            for (int i = 0; i < index; i++)
                x = x.next;
            return x;
        } else {
            Node<E> x = last;
            for (int i = size - 1; i > index; i--)
                x = x.prev;
            return x;
        }

从 index<(sinze>>1)(size除以2)这句看出,在查找指定位置元素时,是对list从中间进行分割,然后遍历。















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值