• java集合--LinkedList源码


    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;
            }
        }

    尾插方法新增节点;只判断了链表是否为空,非空的话向后增加节点,没有数量限制,所以链表的容量可以无线延伸,只受限于内存的大小

    /**
         * Appends the specified element to the end of this list.
         *
         * <p>This method is equivalent to {@link #addLast}.
         *
         * @param e element to be appended to this list
         * @return {@code true} (as specified by {@link Collection#add})
         */
        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++; }

    remove方法提供了移除链表头结点,尾节点,特定节点,特定index位置的节点;移除首次出现的特定值的节点的方法如下

    /**
         * Removes the first occurrence of the specified element from this list,
         * if it is present.  If this list does not contain the element, it is
         * unchanged.  More formally, removes the element with the lowest index
         * {@code i} such that
         * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>
         * (if such an element exists).  Returns {@code true} if this list
         * contained the specified element (or equivalently, if this list
         * changed as a result of the call).
         *
         * @param o element to be removed from this list, if present
         * @return {@code true} if this list contained the specified element
         */
        public boolean remove(Object o) {
            if (o == null) {
                for (Node<E> x = first; x != null; x = x.next) {
                    if (x.item == null) {
                        unlink(x);
                        return true;
                    }
                }
            } else {
                for (Node<E> x = first; x != null; x = x.next) {
                    if (o.equals(x.item)) {
                        unlink(x);
                        return true;
                    }
                }
            }
            return false;
        }
    
    /**
         * Unlinks non-null node x.
         */
        E unlink(Node<E> x) {
            // assert x != null;
            final E element = x.item;
            final Node<E> next = x.next;
            final Node<E> prev = x.prev;
    
            if (prev == null) {
                first = next;
            } else {
                prev.next = next;
                x.prev = null;
            }
    
            if (next == null) {
                last = prev;
            } else {
                next.prev = prev;
                x.next = null;
            }
    
            x.item = null;
            size--;
            modCount++;
            return element;
        }

    从代码中可以看出,null被特殊处理了,也就是说,LinkedList可以储存null;

    LinkedList实现了Deque接口,Deque的文档可以参考:https://docs.oracle.com/javase/8/docs/api/java/util/Deque.html

  • 相关阅读:
    开发思路总结
    电脑蓝屏代码丢失,Eclipse这个功能帮我找回了代码
    书籍
    向日葵无法在控制端用键盘输入的原因
    js 多submit 不使其执行其他submit form
    js获取url参数值
    C# 正则表达式 去除HTML标签 C#后台
    读取txt文件 循环操作每行数据 添加到数据库
    Ubuntu中安装XAMPP出错的解决方法(转发,备查)
    常用正则表达式
  • 原文地址:https://www.cnblogs.com/boboshenqi/p/10410887.html
Copyright © 2020-2023  润新知