博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
List源码学习之LinkedList
阅读量:6907 次
发布时间:2019-06-27

本文共 3162 字,大约阅读时间需要 10 分钟。

  LinkedList 内部数据接口为一个链表,存储数据可为空可重复。

1.包含主要参数:

//集合长度 transient int size = 0;/*** 头结点*/transient Node
first;/*** 尾结点*/transient Node
last; //节点结构

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.remove(Object o):移除集合的对象,只移除从头节点开始匹配到的第一个节点

public boolean remove(Object o) {        if (o == null) {            for (Node
x = first; x != null; x = x.next) { if (x.item == null) { unlink(x); return true; } } } else { for (Node
x = first; x != null; x = x.next) { if (o.equals(x.item)) { unlink(x); return true; } } } return false; }
E unlink(Node
x) {
// assert x != null; final E element = x.item; final Node
next = x.next; final Node
prev = x.prev;    //判断前一个节点,若为空说明此节点为first if (prev == null) {
first = next; } else {
   //前一个节点连接当前节点的下一个节点 prev.next = next;    //去掉当前节点的前连接 x.prev = null; }    //判断后一个节点,若为空说明当前节点为last if (next == null) {
last = prev; } else {
   //当前节点的下一个节点连接上一个节点 next.prev = prev;   //去除当前节点的后连接 x.next = null; } x.item = null; size--; modCount++; return element; }
 

3.get(int index):根据下标获取对象,根据当前集合的size二分后与index比较,判断从头节点还是尾节点开始循环找目标节点。

public E get(int index) {        checkElementIndex(index);        return node(index).item;    }Node
node(int index) {
if (index < (size >> 1)) { Node
x = first; for (int i = 0; i < index; i++) x = x.next; return x; } else { Node
x = last; for (int i = size - 1; i > index; i--) x = x.prev; return x; } }

4.add(int index,Object o):插入至具体位置

public void add(int index, E element) {        checkPositionIndex(index);        if (index == size)            linkLast(element);        else            linkBefore(element, node(index));    }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++; }void linkBefore(E e, Node
succ) { // assert succ != null; final Node
pred = succ.prev; final Node
newNode = new Node<>(pred, e, succ); succ.prev = newNode; if (pred == null) first = newNode; else pred.next = newNode; size++; modCount++; }

 进阶之路从此开始,路在脚下早已铺好,走与不走全看你想不想欣赏沿途的风景———————AmbitiousMice

转载于:https://www.cnblogs.com/AmbitiousMice/p/8064407.html

你可能感兴趣的文章
怎么注册tk域名_域名注册后怎么做网站?有了域名如何搭建网站?
查看>>
routing zuul_Zuul网关
查看>>
见缝插针的人_菁华语学法 修六和敬 见缝插针 基本功训练(下)——修六和敬 见缝插针 基本功训练...
查看>>
java位运算符取反_Java逻辑运算符,位运算符
查看>>
lightgbm过去版本安装包_OLT版本升级操作指南
查看>>
idea中的java文件是j状态_JVM中必须要掌握的java的.class文件的加载过程
查看>>
docker 制作本地镜像_每天5分钟|轻松掌握开发必会的docker套路-制作自己的镜像...
查看>>
打包css合并_编写灵活、稳定、高质量的CSS代码的规范
查看>>
库 keil 编译很慢_CmBacktrace: ARM CortexM 系列 MCU 错误追踪库
查看>>
订阅多个主题_SpringBoot整合Redis,怎么实现发布/订阅?
查看>>
tcl自动保存结果expect_TCL/Expect读取配置文件内容
查看>>
runaction 旋转_Cocos Creator 中的动作系统那些事儿
查看>>
比亚迪汉搭载鸿蒙系统和麒麟芯片_官宣!搭载鸿蒙系统和鸿鹄芯片,华为荣耀在东莞发布首款“智慧屏”!...
查看>>
html接收model值_v-bind和v-model的区别
查看>>
java继承两个类_Java入门第十六课:如何用继承的方法定义类
查看>>
mysql创建函数_MySQL 创建函数
查看>>
pdm 导入mysql 注释_PowerDesigner逆向导入MYSQL数据库并显示中文注释
查看>>
国二MySQL考些啥_国二考试时间 国二考试是什么
查看>>
js mysql替换_JS replaceChild()方法:替换节点
查看>>
导出pdf 不换行_公众号文章导出为PDF步骤详解,让文章导出不变形的小秘密
查看>>