Java源码解析LinkedList
作者:李灿辉 发布时间:2023-04-28 02:56:18
标签:java,linkedlist
本文基于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链表(Linked List)基本原理与实现方法。分享给大家供大家参考,具体如下:在分析链表之前,我们先来对之前的动态
- 本系列代码地址:https://github.com/JoJoTec/spring-cloud-parentOpenFeign 的由来和实现
- react native打包apk文件安装好之后进入应用闪退这个是我一个前端前辈帮我弄的,自己解决的时候不行,她去官网找了相关的问题,然后发
- 我们知道,多线程是Android开发中必现的场景,很多原生API和开源项目都有多线程的内容,这里简单总结和探讨一下常见的多线程切换方式。我们
- cin的返回值今天在用STL时用到while(cin>>s1>>a>>s2>>b)这样的语句
- 一、运算符运算符包括下面几种:算术运算符赋值运算符比较运算符逻辑运算符位运算符三目运算符最不常用的是位运算符,但也是最接近计算机底层的。1、
- 当你在使用Mybatis 时进行配置的时候有这样几个坑一定要注意下。mybatisplus中逻辑删除通俗说为了在数据库中保留数据,但是又不想
- 加密解密本身并不是难事,问题是在何时去处理?定义一个过滤器,将请求和响应分别拦截下来进行处理也是一个办法,这种方式虽然粗暴,但是灵活,因为可
- Java 理解 ThreadLocal摘要: ThreadLocal 又名线程局部变量,是 Java 中一种较为特殊的线程绑定机制,用于保证
- Spring框架基于注解开发CRUD,供大家参考,具体内容如下1. Maven坐标<!-- https://mvnrepository
- 字节流和字符流对于文件必然有读和写的操作,读和写就对应了输入和输出流,流又分成字节和字符流。1.从对文件的操作来讲,有读和写的操作——也就是
- 1.首先解释一下什么是方法重载?方法重载是指在同一个类中方法同名,参数不同,调用时根据实参的形式,选择与他匹配的方法执行操作的一种技术。这里
- 一、layui.use1、LayUI的官方使用文档:https://www.layui.com/doc/2、layui的内置模块不是默认就加
- 写在前面现在,越来越多的App里面使用了模糊效果,这种模糊效果称之为高斯模糊。大家都知道,在Android平台上进行模糊渲染是一个相当耗CP
- 前言我昨天做了个梦,我梦见我在一条路走,走的时候经过一个房间,里面关着一条边牧和鸡和猪,后来我醒了,我知道那只边牧就是小叶子(哈仔十一的边牧
- 本文实例讲述了Android编程中沉浸式状态栏的三种实现方式。分享给大家供大家参考,具体如下:沉浸式状态栏Google从android ki
- 一、介绍Mesh类:通过脚本创建或是获取网格的类,网格包含多个顶点和三角形数组。顶点信息包含坐标和所在面的法线。unity中3D的世界的所有
- 一、引言使用原生的zookeeper时候会遇到watcher一次注册生效一次等情况,因此使用curatorcurator是Netflix公司
- DAO层测试难点可重复性,每次运行单元测试,得到的数据是重复的独立性,测试数据与实际数据相互独立数据库中脏数据预处理不能给数据库中数据带来变
- 四个主要操作类:JsonConverter 、JsonHelper 、JsonSplit 、AjaxResult一、JsonConverte