zoukankan      html  css  js  c++  java
  • jdk 1.7 LinkedList 源码分析

    1 linkedList 的定义
     
    public class LinkedList<E>
        extends AbstractSequentialList<E>
        implements List<E>, Deque<E>, Cloneable, java.io.Serializable
     
    从这段代码中我们可以清晰地看出LinkedList继承AbstractSequentialList,实现List、Deque、Cloneable、Serializable。其中AbstractSequentialList提供了 List 接口的骨干实现,从而最大限度地减少了实现受“连续访问”数据存储(如链接列表)支持的此接口所需的工作,从而以减少实现List接口的复杂度。Deque一个线性 collection,支持在两端插入和移除元素,定义了双端队列的操作。
     
    从中可以看出LinkedList既实现了List接口也实现了双向队列的接口
     
    2、属性
     
    transient int size = 0;  
    transient Node<E> first; 
    transient Node<E> last;
     
    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;
            }
        }
     
    size 记录链表中的节点个数,LinkedList是个双向链表所以有前后两个节点,在LinkedList里有个内部类即节点类node
     
    3、构造方法
     
      public LinkedList() {
        }
    默认的构造方法内不做任何操作
     
        public LinkedList(Collection<? extends E> c) {
            this();
            addAll(c);
        }
     
      public boolean addAll(Collection<? extends E> c) {
            return addAll(size, c); 
        }
     
     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;
        }
     
    参数是集合的构造函数,他调用了addAll(c) 方法,addAll(c)中有调用了addAll(size, c)方法,size代表的是插入的下标位置,然后为每个对象一个一个地创造node类,然后进行链表插入操作,addAll()方法中调用了node(index)函数,该函数是查找到index下标位置的节点并返回该节点
     
     Node<E> node(int index) {
            // assert isElementIndex(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在前半段还是在后半段,若在前半段用first遍历 若在后半段用last遍历
     
     
    4、增加方法
     
     public boolean add(E e) {
            linkLast(e);
            return true;
        }
     
     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++;
        }
     
    直接调用add()方法参数是一个对象,实际上是在链表最后加上新的节点,让last指向新的节点
     
     public void add(int index, E element) {
            checkPositionIndex(index);
     
            if (index == size)
                linkLast(element);
            else
                linkBefore(element, node(index));
        }
     
     void linkBefore(E e, Node<E> succ) {
            // assert succ != null;
            final Node<E> pred = succ.prev;
            final Node<E> newNode = new Node<>(pred, e, succ);
            succ.prev = newNode;
            if (pred == null)
                first = newNode;
            else
                pred.next = newNode;
            size++;
            modCount++;
        }
    在指定下标位置插入新的对象,先判断index是否有效,有效位置为0-size , 如果为size 直接调用add()函数加到链表尾,否则调用 linkBefore()函数
     
    其他的还有
    public boolean addAll(Collection<? extends E> c) {
            return addAll(size, c);
        }
    ////////////////////////////////////////往链表前加入新的对象
     public void addFirst(E e) {
            linkFirst(e);
        }
     
     private void linkFirst(E e) {
            final Node<E> f = first;
            final Node<E> newNode = new Node<>(null, e, f);
            first = newNode;
            if (f == null)
                last = newNode;
            else
                f.prev = newNode;
            size++;
            modCount++;
        }
    //////////////////////////////////////////////////////////////////////////////////////////
     在链表后加入新的节点
     public void addLast(E e) {
            linkLast(e);
        }
      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++;
        }

    5、删除方法
     
      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;
        }
     
     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,删除的是最小index符合条件的对象并不是删除链表中满足条件的所有对象
     
     public E remove(int index) {
            checkElementIndex(index);
            return unlink(node(index));
        }
     
    删除在index位置上的对象
     
    其他方法还有:
    removeFirst() 删除头结点
    removeLast() 删除末尾节点
    removeFirstOccurrence(Object o) 删除从头到尾遍历是o的第一个对象 实际上调用的是remove(Object o) 两者是一个概念
    removeLastOccurrence(Object o) 删除从尾到头遍历是o的第一个对象
     
     
     
    6、查找方法
     
     public E get(int index) {
            checkElementIndex(index);
            return node(index).item;
        }
     
    返回下标位置为index的对象内容
     
     public E getFirst() {
            final Node<E> f = first;
            if (f == null)
                throw new NoSuchElementException();
            return f.item;
        }
    返回头结点内容
     
     public E getLast() {
            final Node<E> l = last;
            if (l == null)
                throw new NoSuchElementException();
            return l.item;
        }
     
    返回尾节点的内容
     
     
    7序列化与反序列化
     
    从定义中LinkedList实现了serializable 所以可以进行序列化 ,在源码中有对序列化和反序列化的函数进行重写
     
     private void writeObject(java.io.ObjectOutputStream s)
            throws java.io.IOException {
            // Write out any hidden serialization magic
            s.defaultWriteObject();
     
            // Write out size
            s.writeInt(size);
     
            // Write out all elements in the proper order.
            for (Node<E> x = first; x != null; x = x.next)
                s.writeObject(x.item);
        }
     
       
     
       
        private void readObject(java.io.ObjectInputStream s)
            throws java.io.IOException, ClassNotFoundException {
            // Read in any hidden serialization magic
            s.defaultReadObject();
     
            // Read in size
            int size = s.readInt();
     
            // Read in all elements in the proper order.
            for (int i = 0; i < size; i++)
                linkLast((E)s.readObject());
        }
    }
     
    戒骄戒躁,一步一个脚印
  • 相关阅读:
    bzoj 4897 天赋 有向图的矩阵数定理
    bzoj 4621 Tc605 思想+dp
    bzoj 4596 [Shoi2016]黑暗前的幻想乡 矩阵树定理+容斥
    bzoj 4455 [Zjoi2016]小星星 树形dp&容斥
    获取Android设备无线和以太网MAC地址
    Java WebSocket库:https://github.com/TooTallNate/Java-WebSocket
    UsbManager, UsbDevice的简单示例
    Android开发之开机自动启动应用
    使用Microsoft Office 2007将文档转换为PDF
    C/C++实现删除字符串的首尾空格
  • 原文地址:https://www.cnblogs.com/sophelia-M/p/4777380.html
Copyright © 2011-2022 走看看