zoukankan      html  css  js  c++  java
  • jdk源码阅读笔记之java集合框架(四)(LinkedList)

    关于LinkedList的分析,会从且仅从其添加(add)方法入手。

    因为上一篇已经分析过ArrayList,相似的地方就不再叙述,关注点在LinkedList的特点。

    属性:

       /**
         *链表头
         */
        transient Node<E> first;
    
        /**
         * 链表尾
         */
        transient Node<E> last;
    View Code

    从以上两个属性可以得出LinkedList是基于双向链表实现的。

    节点代码(无需过多解释):

     private static class Node<E> {
            E item;
            Node<E> next;
            Node<E> prev;
    
            Node(Node<E> prev, E element, Node<E> next) {
                this.item = element;
                this.next = next;
                this.prev = prev;
            }
        }
    View Code

    LinkedList的add方法:

    LinkedList中有多个add方法,大同小异,遂选取其一进行分析。

    /**
         * 插入指定元素到list尾部
         */
        public boolean add(E e) {
            linkLast(e);
            return true;
        }
        /**
         * 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++;//此系列博客中有详细解释
        }
  • 相关阅读:
    python中的内置函数的思维导图
    练习(面试题):关于生成器函数的求和问题
    推导式, 生成器表达式
    生成器
    静态代码块
    java中内存的划分
    静态方法
    Chapter07Scanner类、Random类、ArrayList类
    泛型
    对象数组
  • 原文地址:https://www.cnblogs.com/jw93/p/6846684.html
Copyright © 2011-2022 走看看