当前位置:网站首页>双端链表LinkedList
双端链表LinkedList
2022-08-07 00:59:00 【努 力 小 子】
基本概念
LinkedList实现了List接口,底层的链表结构使它支持高效的插入和删除操作。
实现了Deque接口,具有队列的特性。
LinkedList不是线程安全的,如果想使LinkedList变成线程安全(在拥有共享数据的多条线程并行执行的程序中,线程安全的代码会通过同步机制保证各个线程都可以正常且正确的执行,不会出现数据污染等意外情况),可以调用静态类Collections类中的synchronizedList方法:
List list=Collections.synchronizedList(new LinkedList(...));
结构组成

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

基本方法
add(E e) 方法:将元素添加到链表尾部。
add(int index,E e):在指定位置添加元素。
addAll(Collection c ):将集合插入到链表尾部。
addAll(int index, Collection c): 将集合从指定位置开始插入。
addFirst(E e): 将元素添加到链表头部。
addLast(E e): 将元素添加到链表尾部,与 add(E e) 方法一样。
get(int index): 根据指定索引返回数据。
getFirst(),element(),peek(),peekFirst(): 获取头节点(index=0)数据。getFirst() 和element() 方法将会在链表为空时,抛出异常NoSuchElementException,element()方法的内部就是使用getFirst()实现的。
获取尾节点(index=-1)数据方法: getLast() 方法在链表为空时,会抛出NoSuchElementException,而peekLast() 则不会,只是会返回 null。
根据对象得到索引的方法:int indexOf(Object o): 从头遍历找,int lastIndexOf(Object o): 从尾遍历找。
检查链表是否包含某对象:contains(Object o): 检查对象o是否存在于链表中。
删除:remove() ,removeFirst(),pop(): 删除头节点,removeLast(),pollLast(): 删除尾节点,removeLast()在链表为空时将抛出NoSuchElementException,而pollLast()方法返回null。remove(int index):删除指定位置的元素。
边栏推荐
猜你喜欢

论文解读《PCT: Point cloud transformer》

元器件正反(极性)检测案例

【华为云至简致远】还在烦恼成本高、运维难?华为云数据库给你一个标准答案!

登顶KITTI!Mix-Teaching:适用于单目3D目标检测的半监督方法【清华大学】

2022年G2电站锅炉司炉题库及在线模拟考试

Vi learning (2) the common commands include move cursor/highlight the text/undo and the undo/delete/copy and paste the text/find replacement/insert 】

普林斯顿微积分读本04第三章--极限导论

Usage and simulation implementation of library functions such as strcmp, strstr, memcpy, and memmove

蒸馏学习框架小抄(1)

2022 Electrician (Elementary) Special Work Certificate Exam Question Bank and Mock Exam
随机推荐
Introduction to Ftrace function graph
【Redis】Redis学习——三种特殊数据类型
[7] Advanced C language -- program compilation (preprocessing operation) + linking
禁用防火墙后,aria2的6800端口还是不通
view function index questions
Hand torn Android Framework bottom-level interview questions collection
测试工程师转开发希望大吗?
[1408. String matching in arrays]
CompletableFuture使用示例
视图函数索引题
AtCoder Beginner Contest 263(A~D) ect
一文了解DataStore(Proto)
存储中的爱情:cookie、本地存储、会话存储
union联合体的理解(接上篇数据类型float)
定义一个函数,接收三个参数返回一元二次方程
Detailed explanation of C51 basic functions, interrupt functions and library functions
CV领域经典backbone模型小抄(2)
服务端没有 listen,客户端发起连接建立,会发生什么?
2022危险化学品经营单位安全管理人员考试题模拟考试题库及模拟考试
jvm总结