博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LinkedList 源码小解
阅读量:4700 次
发布时间:2019-06-09

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

一、成员private transient Entry
header = new Entry
(null, null, null);private transient int size = 0;底层维护的是一个Entry链表(双向循环链表)二、LinkedList.Entry类成员E element; //dataEntry
next; //前指针Entry
previous; //后指针三、方法1、public LinkedList() {header.next = header.previous = header;}该方法构造了一个新的Entry链表,前后指针都指向自身2、public E getFirst() {if (size==0)throw new NoSuchElementException();return header.next.element;}获取链表中第一个元素3、public E getLast() {if (size==0)throw new NoSuchElementException();return header.previous.element;}获取链表中最后一个元素4、private E remove(Entry
e) {if (e == header)throw new NoSuchElementException();E result = e.element;e.previous.next = e.next;e.next.previous = e.previous;e.next = e.previous = null;e.element = null;size--;modCount++;return result;}移除链表中的某个元素5、private Entry
addBefore(E e, Entry
entry) {Entry
newEntry = new Entry
(e, entry, entry.previous);newEntry.previous.next = newEntry;newEntry.next.previous = newEntry;size++;modCount++;return newEntry;}插入一个元素 6、public boolean addAll(int index, Collection
c) {if (index < 0 || index > size)throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);Object[] a = c.toArray();int numNew = a.length;if (numNew==0)return false;modCount++;Entry
successor = (index==size ? header : entry(index));Entry
predecessor = successor.previous;for (int i=0; i
e = new Entry
((E)a[i], successor, predecessor);predecessor.next = e;predecessor = e;}successor.previous = predecessor;size += numNew;return true;}在链表最后面添加7、private Entry
entry(int index) {if (index < 0 || index >= size)throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);Entry
e = header;if (index < (size >> 1)) {for (int i = 0; i <= index; i++)e = e.next;} else {for (int i = size; i > index; i--)e = e.previous;}return e;}获得index对应的Entry对象,如果index>size>>1则使用前指针,如果index
>1 则使用后指针遍历到索引处8、public int indexOf(Object o) {int index = 0;if (o==null) {for (Entry e = header.next; e != header; e = e.next) {if (e.element==null)return index;index++;}} else {for (Entry e = header.next; e != header; e = e.next) {if (o.equals(e.element))return index;index++;}}return -1;}获取元素对应的索引位置9、public boolean removeLastOccurrence(Object o) {if (o==null) {for (Entry
e = header.previous; e != header; e = e.previous) {if (e.element==null) {remove(e);return true;}}} else {for (Entry
e = header.previous; e != header; e = e.previous) {if (o.equals(e.element)) {remove(e);return true;}}}return false;}移除最后一次出现的元素10、 private class ListItr implements ListIterator
list中用来遍历的iterator类型11、public Iterator
descendingIterator() {return new DescendingIterator();}private class DescendingIterator implements Iterator用于倒序遍历的iterator

 

转载于:https://www.cnblogs.com/lige-H/p/7392258.html

你可能感兴趣的文章
TCP之1460MSS和1448负载
查看>>
自定义博客样式
查看>>
mac安装 配置 ant
查看>>
[译]快照技术综述 Ⅰ
查看>>
vSphere 高级特性FT配置与管理
查看>>
mac find桌面显示desktop问题
查看>>
Computer Systems A Programmer's Perspective(深入理解计算机系统)第一章读书笔记
查看>>
语义分析
查看>>
mybatis之xml中日期时间段查询的sql语句
查看>>
污染物在线自动监控(监测)系统数据传输标准 (HJ212-2017)-空气质量监测数据包构造...
查看>>
【Python】django模型models的外键关联使用
查看>>
httperf ---linux web站点压力测试
查看>>
JAVA基础知识(五)数据类型转换
查看>>
hdu-5583 Kingdom of Black and White(数学,贪心,暴力)
查看>>
算法与设计模式
查看>>
(4)理解 neutron ml2---port创建流程代码解析
查看>>
python List
查看>>
Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'userinfo.
查看>>
免费资源:Polaris UI套件 + Linecons图标集(AI, PDF, PNG, PSD, SVG)
查看>>
http响应状态码大全
查看>>