zoukankan      html  css  js  c++  java
  • List源码学习之LinkedList

      LinkedList 内部数据接口为一个链表,存储数据可为空可重复。

    1.包含主要参数:

    //集合长度
    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;
      }
    }

    2.remove(Object o):移除集合的对象,只移除从头节点开始匹配到的第一个节点

    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;
       //判断前一个节点,若为空说明此节点为first
    if (prev == null) {
    first = next;
    } else {
       //前一个节点连接当前节点的下一个节点
    prev.next = next;
       //去掉当前节点的前连接
    x.prev = null;
    }
       //判断后一个节点,若为空说明当前节点为last
    if (next == null) {
    last = prev;
    } else {
       //当前节点的下一个节点连接上一个节点
    next.prev = prev;
      //去除当前节点的后连接
    x.next = null;
    }

    x.item = null;
    size--;
    modCount++;
    return element;
    }
     

    3.get(int index):根据下标获取对象,根据当前集合的size二分后与index比较,判断从头节点还是尾节点开始循环找目标节点。

    public E get(int index) {
            checkElementIndex(index);
            return node(index).item;
        }
    Node<E> node(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;
            }
        }

    4.add(int index,Object o):插入至具体位置

    public void add(int index, E element) {
            checkPositionIndex(index);
    
            if (index == size)
                linkLast(element);
            else
                linkBefore(element, node(index));
        }
    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++;
        }
    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++;
        }

     进阶之路从此开始,路在脚下早已铺好,走与不走全看你想不想欣赏沿途的风景———————AmbitiousMice

  • 相关阅读:
    监控页面所有checkbox改变状态的简单方法
    poorman’sgraphicalboot
    Linux 高精確的時序(sleep, usleep,nanosleep)
    【转】跟我一起写udev规则(译)
    什么情况下可以不创建QCoreApplication
    Linux双网卡bonding举例
    many former solutions has been "deleted"
    文件浏览器
    配置站点集的配额和锁
    HyperV的三种网卡
  • 原文地址:https://www.cnblogs.com/AmbitiousMice/p/8064407.html
Copyright © 2011-2022 走看看