Java源码解析LinkedList

所属分类: 软件编程 / java 阅读数: 23
收藏 0 赞 0 分享

本文基于jdk1.8进行分析。

LinkedList和ArrayList都是常用的java集合。ArrayList是数组,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;
    }
  }

成员变量如下。它有头节点和尾节点2个指针。

  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;

下面看一下主要方法。首先是get方法。如下图。链表的get方法效率很低,这一点需要注意,也就是说,我们可以用for循环get(i)的方式去遍历ArrayList,但千万不要这样去遍历Linkedlist。因为Linkedlist进行get时,需要把从头结点或尾节点一个一个的找到第i个元素,效率很低。遍历LinkedList时应该使用foreach方式。

  /**
   * Returns the element at the specified position in this list.
   * @param index index of the element to return
   * @return the element at the specified position in this list
   * @throws IndexOutOfBoundsException {@inheritDoc}
   **/
  public E get(int index) {
    checkElementIndex(index);
    return node(index).item;
  }
  /**
   * Returns the (non-null) Node at the specified element 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;
    }
  }

下面是add方法,add方法把待添加的元素添加到链表末尾即可。

  /**
   * 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++;
  }

This is the end。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

更多精彩内容其他人还在看

Collections工具类_动力节点Java学院整理

Collections工具类提供了大量针对Collection/Map的操作。这篇文章主要介绍了Collections工具类_动力节点Java学院整理,需要的朋友可以参考下
收藏 0 赞 0 分享

SpringMVC集成Swagger实例代码

本篇文章主要介绍了SpringMVC集成Swagger实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

十大常见Java String问题_动力节点Java学院整理

本文介绍Java中关于String最常见的10个问题,需要的朋友参考下吧
收藏 0 赞 0 分享

Java微信公众平台开发(13) 微信JSSDK中Config配置

这篇文章主要为大家详细介绍了Java微信公众平台开发第十三步,微信JSSDK中Config配置,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java实现一个达达租车系统的步骤详解

这篇文章主要给大家介绍了利用Java实现一个达达租车系统的步骤,文中给出了详细的实现思路和示例代码,并在文末给出了完整的源码供大家学习下载,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

Java微信公众平台开发(14) 微信web开发者工具使用

这篇文章主要为大家详细介绍了Java微信公众平台开发第十四步,微信web开发者工具的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Spring Boot整合RabbitMQ实例(Topic模式)

Topic Exchange 转发消息主要是根据通配符。接下来通过本文给大家分享Spring Boot整合RabbitMQ实例(Topic模式),需要的朋友参考下吧
收藏 0 赞 0 分享

Java微信公众平台开发(15) 微信JSSDK的使用

这篇文章主要为大家详细介绍了Java微信公众平台开发第十五步,微信JSSDK的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java多线程的同步方法实例代码

这篇文章主要介绍了 java多线程的同步方法实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

spring boot整合RabbitMQ实例详解(Fanout模式)

这篇文章主要介绍了spring boot整合RabbitMQ的实例讲解(Fanout模式),非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多