【Java笔记】LinkedList 底层结构

一、LinkedList 的全面说明

  1. LinkedList底层实现了双向链表和双端队列特点
  2. 可以添加任意元素(元素可以重复),包括null
  3. 线程不安全,没有实现同步

二、LinkedList 的底层操作机制

三、LinkedList的增删改查案例

public class LinkedListCRUD {

public static void main(String[] args) {

LinkedList linkedList = new LinkedList();

linkedList.add(1);

linkedList.add(2);

linkedList.add(3);

System.out.println("linkedList=" + linkedList);

//演示一个删除结点的

linkedList.remove(); // 这里默认删除的是第一个结点

//linkedList.remove(2);

System.out.println("linkedList=" + linkedList);

//修改某个结点对象

linkedList.set(1, 999);

System.out.println("linkedList=" + linkedList);

//得到某个结点对象

//get(1) 是得到双向链表的第二个对象韩顺平循序渐进学 Java 零基础

第 636页

Object o = linkedList.get(1);

System.out.println(o);//999

//因为 LinkedList 是 实现了 List 接口, 遍历方式

System.out.println("=LinkeList 遍历迭代器==");

Iterator iterator = linkedList.iterator();

while (iterator.hasNext()) {

Object next = iterator.next();

System.out.println("next=" + next);

}

System.out.println("=LinkeList 遍历增强 for==");

for (Object o1 : linkedList) {

System.out.println("o1=" + o1);

}

System.out.println("=LinkeList 遍历普通 for==");

for (int i = 0; i < linkedList.size(); i++) {

System.out.println(linkedList.get(i));

}

//老韩源码阅读. /* 1. LinkedList linkedList = new LinkedList();

public LinkedList() {}

  1. 这时 linkeList 的属性 first = null last = null韩顺平循序渐进学 Java 零基础

  2. 执行 添加

public boolean add(E e) {

linkLast(e);

return true;

}

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

void linkLast(E e) {

final Node l = last;

final Node newNode = new Node<>(l, e, null);

last = newNode;

if (l == null)

first = newNode;

else

l.next = newNode;

size++;

modCount++;

}
/
/

老韩读源码 linkedList.remove(); // 这里默认删除的是第一个结点

  1. 执行 removeFirst
    public E remove() {
    return removeFirst();
    }韩顺平循序渐进学 Java 零基础
    第 638页
  2. 执行
    public E removeFirst() {
    final Node f = first;
    if (f == null)
    throw new NoSuchElementException();
    return unlinkFirst(f);
    }
  3. 执行 unlinkFirst, 将 f 指向的双向链表的第一个结点拿掉
    private E unlinkFirst(Node f) {
    // assert f == first && f != null;
    final E element = f.item;
    final Node 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;
    }
    */
    }
    }
相关推荐
ZhengEnCi9 分钟前
@Parameter 注解技术解析-从 API 文档生成到接口描述清晰的 SpringBoot 利器
java·spring boot
低调小一12 分钟前
Kuikly 小白拆解系列 · 第1篇|两棵树直调(Kotlin 构建与原生承载)
android·开发语言·kotlin
郝学胜-神的一滴16 分钟前
Linux下的阻塞与非阻塞模式详解
linux·服务器·开发语言·c++·程序人生·软件工程
AresXue18 分钟前
2025最新Java性能优化建议 应用 数据库 机器 网络
java
跟着珅聪学java27 分钟前
spring boot 整合 activiti 教程
android·java·spring
yanqiaofanhua28 分钟前
C语言自学--预处理详解
c语言·开发语言
一只小风华~30 分钟前
学习笔记:Vue Router 中的链接匹配机制与样式控制
前端·javascript·vue.js·笔记·学习·ecmascript
沐知全栈开发36 分钟前
Vue3 计算属性
开发语言
冰糖雪梨dd1 小时前
JS中new的过程发生了什么
开发语言·javascript·原型模式
junnhwan2 小时前
【苍穹外卖笔记】Day04--套餐管理模块
java·数据库·spring boot·后端·苍穹外卖·crud