zoukankan      html  css  js  c++  java
  • LinkedList源码疑问记录

          早上看linkedList源码时候,对于它的初始化一直不太明白。如下:

        

     transient int size = 0;
    
        /**
         * Pointer to first node.
         * Invariant: (first == null && last == null) ||
         *            (first.prev == null && first.item != null)
         */
        transient Node<E> first;
    
        /**
         * Pointer to last node.
         * Invariant: (first == null && last == null) ||
         *            (last.next == null && last.item != null)
         */
        transient Node<E> last;
      /**
         * Inserts all of the elements in the specified collection into this
         * list, starting at the specified position.  Shifts the element
         * currently at that position (if any) and any subsequent elements to
         * the right (increases their indices).  The new elements will appear
         * in the list in the order that they are returned by the
         * specified collection's iterator.
         *
         * @param index index at which to insert the first element
         *              from the specified collection
         * @param c collection containing elements to be added to this list
         * @return {@code true} if this list changed as a result of the call
         * @throws IndexOutOfBoundsException {@inheritDoc}
         * @throws NullPointerException if the specified collection is null
         */
        public boolean addAll(int index, Collection<? extends E> c) {
            checkPositionIndex(index);
    
            Object[] a = c.toArray();
            int numNew = a.length;
            if (numNew == 0)
                return false;
    
            Node<E> pred, succ;
            if (index == size) {
                succ = null;
                pred = last;
            } else {
                succ = node(index);
                pred = succ.prev;
            }
    
            for (Object o : a) {
                @SuppressWarnings("unchecked") E e = (E) o;
                Node<E> newNode = new Node<>(pred, e, null);
                if (pred == null)
                    first = newNode;
                else
                    pred.next = newNode;
                pred = newNode;
            }
    
            if (succ == null) {
                last = pred;
            } else {
                pred.next = succ;
                succ.prev = pred;
            }
    
            size += numNew;
            modCount++;
            return true;
        }

      一直不明白怎么赋值的。跟着代码调试后,刚开始是null,使用的时候每次add,然后再去赋值。大概过程如下:

    另一个构造方法是带Collection值得对象作为入参的构造函数的,下面是执行逻辑:

    1)使用this()调用默认的无参构造函数。

    2)调用addAll()方法,传入当前的节点个数size,此时size为0,并将collection对象传递进去

    3)检查index有没有数组越界的嫌疑

    4)将collection转换成数组对象a

    5)循环遍历a数组,然后将a数组里面的元素创建成拥有前后连接的节点,然后一个个按照顺序连起来。

    6)修改当前的节点个数size的值

    7)操作次数modCount自增1.

    剩余的慢慢看~

  • 相关阅读:
    前端通过ajax请求,调用net core webapi接口
    WeUI——手机验证码
    Docker学习笔记之--安装mssql(Sql Server)并使用Navicat连接测试(环境:centos7)
    Docker学习笔记之-推送镜像到Registrys仓库
    PuppeteerSharp 在asp.net中使用 PuppeteerSharp生命周期管理
    vscode 插件记录下
    angular 项目8升级9 踩坑
    Skoruba.IdentityServer4.Admin 踩坑
    VUE-MATOMO实现埋点
    netcore rpm
  • 原文地址:https://www.cnblogs.com/thinkingandworkinghard/p/9540073.html
Copyright © 2011-2022 走看看