Java源码解析LinkedList

作者:李灿辉 时间:2023-04-28 02:56:18 

本文基于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。

来源:https://blog.csdn.net/li_canhui/article/details/85004699

标签:java,linkedlist
0
投稿

猜你喜欢

  • Android实现文字垂直滚动、纵向走马灯效果的实现方式汇总

    2023-03-02 22:24:54
  • Java二维码登录流程实现代码(包含短地址生成,含部分代码)

    2021-10-23 02:06:26
  • java多线程Future和Callable类示例分享

    2021-09-02 09:49:37
  • java 将一个数组逆序输出的方法

    2023-05-09 09:29:01
  • Winform中GridView分组排序功能实现方法

    2022-01-29 16:20:03
  • Java NIO实现聊天功能

    2022-06-12 08:31:31
  • kettle中使用js调用java类的方法

    2022-05-09 00:06:31
  • Java热门笔试试题整理

    2023-11-25 08:56:33
  • android自定义简单时钟

    2022-01-10 03:00:22
  • Android对sdcard扩展卡文件操作实例详解

    2023-12-20 11:34:55
  • Android实现WebView点击拦截跳转原生

    2023-01-06 23:47:38
  • java实现双色球抽奖算法

    2023-11-28 23:51:51
  • AOP之事务管理<aop:advisor>的两种配置方式

    2023-11-24 22:55:06
  • Java实战之实现在线小说阅读系统

    2022-10-09 18:15:09
  • ZooKeeper入门教程一简介与核心概念

    2022-11-24 18:36:00
  • SpringBoot实现动态多线程并发定时任务

    2023-12-12 01:58:26
  • 浅析Java中线程的创建和启动

    2022-12-29 17:37:41
  • C# 匿名方法基础回顾

    2023-02-27 13:59:23
  • Java设计模式之模板方法模式详解

    2021-08-04 04:32:51
  • C# 如何解析获取Url参数值

    2022-07-03 01:00:52
  • asp之家 软件编程 m.aspxhome.com