【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;
    }
    */
    }
    }
相关推荐
小羊没烦恼!2 天前
微服务化的基石——持续集成
java·大数据·word·powerpoint·.net
一隅论数智2 天前
给AI一张“业务概念地图“:本体如何从哲学走向企业智能
大数据·人工智能·经验分享·笔记·学习·学习方法·政务
俊昭喜喜里2 天前
java中的继承和多态的区别
java
小羊没烦恼!2 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
譕痕2 天前
JSONObject与JSONArray封装数据格式区别
java·json
胡写代码2 天前
别再前后端各写一套表单校验了
java·后端
小鱼能吃糖2 天前
缺陷修复总览 · mall电商项目:5类缺陷,1个病根,4个业务域
java·电商
此时不提桶,更待何时2 天前
01-06-A-JVM排查实战详解
java·jvm
伞伞悦读2 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
vipxieliang2 天前
ValidX 在 DDD 领域驱动设计中的实践
java·spring boot