Java(LinkedList和ArrayList底层分析)

LinkedList全面说明:
LinkedList底层操作机制:
LinkedList的方法:

add():增加节点对象

remove():删除一个节点对象(默认删除第一个节点对象)

set():修改一个节点对象

get():得到一个节点对象

LinkedList的遍历:

增强for循环

迭代器

普通for循化

LinkedList的源码解读:

增加源码:

  1. LinkedList linkedList = new LinkedList();

public LinkedList() {}

  1. 这时 linkeList 的属性 first = null last = null

  2. 执行 添加

public boolean add(E e) {

linkLast(e);

return true;

}

4.将新的结点,加入到双向链表的最后

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++;

}

删除源码:

  1. 执行 removeFirst

public E remove() {

return removeFirst();

}

  1. 执行

public E removeFirst() {

final Node<E> f = first;

if (f == null)

throw new NoSuchElementException();

return unlinkFirst(f);

}

  1. 执行 unlinkFirst, 将 f 指向的双向链表的第一个结点拿掉

private E unlinkFirst(Node<E> f) {

final E element = f.item;

final Node<E> next = f.next;

f.item = null;

f.next = null; // help GC

first = next;

if (next == null)

last = null;

else

next.prev = null;

size--;

modCount++;

return element;

}

ArrayList 和 LinkedList 比较:
相关推荐
思茂信息7 分钟前
CST软件对Customer Success OPPO手机电源适配器EMC仿真
开发语言·嵌入式硬件·matlab·3d·智能手机·cst
缺点内向1 小时前
如何在 C# 中将 Excel 工作表拆分为多个窗格
开发语言·c#·.net·excel
少废话h2 小时前
解决Flink中ApacheCommonsCLI版本冲突
开发语言·python·pycharm
天命码喽c2 小时前
GraphRAG-2.7.0整合Milvus-2.5.1
开发语言·python·milvus·graphrag
后端小张2 小时前
【JAVA进阶】Spring Boot 核心知识点之自动配置:原理与实战
java·开发语言·spring boot·后端·spring·spring cloud·自动配置
tg-zm8899967 小时前
2025返利商城源码/挂机自动收益可二开多语言/自定义返利比例/三级分销理财商城
java·mysql·php·laravel·1024程序员节
X***C8627 小时前
SpringBoot:几种常用的接口日期格式化方法
java·spring boot·后端
Mr_Xuhhh7 小时前
YAML相关
开发语言·python
咖啡の猫7 小时前
Python中的变量与数据类型
开发语言·python
前端达人7 小时前
你的App消息推送为什么石沉大海?看Service Worker源码我终于懂了
java·开发语言