一、属性及获取属性:
1、size
transient int size = 0; /** * Pointer to first node. * Invariant: (first == null && last == null) || * (first.prev == null && first.item != null) */ transient Nodefirst; /** * Pointer to last node. * Invariant: (first == null && last == null) || * (last.next == null && last.item != null) */ transient Node last;
获取
public int size() { return size; }二、构造函数
//Constructs an empty list public LinkedList() { } public LinkedList(Collection extends E> c) { this(); addAll(c); }三、类
private static class Node四、方法{ E item; Node next; Node prev; Node(Node prev, E element, Node next) { this.item = element; this.next = next; this.prev = prev; } }
1、Node
Nodenode(int index) { // assert isElementIndex(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; } }
*、linkFirst
/** * Links e as first element. */ private void linkFirst(E e) { final Nodef = first; final Node newNode = new Node<>(null, e, f); first = newNode; if (f == null) last = newNode; else f.prev = newNode; size++; modCount++; } 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++; }
*、add、addFirst
public void addFirst(E e) { linkFirst(e); } public boolean add(E e) { linkLast(e); return true; }
linkLast
2、set
public E set(int index, E element) { checkElementIndex(index); Nodex = node(index); E oldVal = x.item; x.item = element; return oldVal; }
3、get
public E get(int index) { checkElementIndex(index); return node(index).item; }
*、clear
public void clear() { // Clearing all of the links between nodes is "unnecessary", but: // - helps a generational GC if the discarded nodes inhabit // more than one generation // - is sure to free memory even if there is a reachable Iterator for (Nodex = first; x != null; ) { Node next = x.next; x.item = null; x.next = null; x.prev = null; x = next; } first = last = null; size = 0; modCount++; }
*、Push Pop
public void push(E e) { addFirst(e); }
public E pop() { return removeFirst(); }
*、node(int index)
Nodenode(int index) { // assert isElementIndex(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; } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/73424.html
摘要:介绍是线程不安全的,允许元素为的双向链表。构造方法共有两个构造方法,一个是创建一个空的构造函数,一个是将已有的添加到中。是将元素插入到的头部。下一篇文章继续分析上次分析了的结构和添加方法这次开始分析下面的。注意源码版本为直接进入正题。 如果本文中有不正确的地方请指出由于没有留言可以在公众号添加我的好友共同讨论。 1.介绍 LinkedList 是线程不安全的,允许元素为null的双向链...
摘要:在次操作中其实即尾节点是共享资源,当多个线程同时执行此方法的时候,其实会出现线程安全问题。同样会出现并发安全问题,下面对此问题进行分析。 1.LinkedList源码分析 LinkedList的是基于链表实现的java集合类,通过index插入到指定位置的时候使用LinkedList效率要比ArrayList高,以下源码分析是基于JDK1.8. 1.1 类的继承结构 LinkedLis...
摘要:它们会在链表为空时,抛出获取尾节点数据方法两者区别方法在链表为空时,会抛出,而则不会,只是会返回。 目录: 0-1. 简介 0-2. 内部结构分析 0-3. LinkedList源码分析 0-3-1. 构造方法 0-3-2. 添加add方法 0-3-3. 根据位置取数据的方法 0-3-4. 根据对象得到索引的方法 0-3-5. 检查链表是否包含某对象的方法 ...
摘要:表明该类是可以序列化的。与对比并没有实现,而实现表明其支持快速通常是固定时间随机访问。此接口的主要目的是允许一般的算法更改其行为,从而在将其应用到随机或连续访问列表时能提供良好的性能。这是随机访问效率低的原因之一。指定节点不能为。 总览 showImg(https://segmentfault.com/img/bVbsIzr?w=1007&h=600); 定义 public class...
摘要:一源码分析本文分析双向链表的查询操作源码实现。中源程序中,的查询操作,通过函数实现。源程序中使用循环进行遍历。表示链表元素索引,初值为。针对空元素的情况,用循环遍历,查找元素为的节点,并返回索引。 一、contains源码分析 本文分析双向链表LinkedList的查询操作源码实现。jdk中源程序中,LinkedList的查询操作,通过contains(Object o)函数实现。具体...
摘要:源码分析是一个双向链表的数据结构实现。对于支持随机访问数据的比如数组,应该优先使用。一个有序的集合支持在头和尾进行插入和删除元素。的大多实现元素数量是没有大小限制的。构造方法第一个是一个空的构造器,第二个构造器调用了方法。 LinkedList源码分析 LinkedList是一个双向链表的数据结构实现。 类的实现接口及继承父类 public class LinkedList exten...
阅读 3737·2021-11-23 09:51
阅读 1401·2021-11-10 14:35
阅读 4040·2021-09-22 15:01
阅读 1309·2021-08-19 11:12
阅读 402·2019-08-30 15:53
阅读 1711·2019-08-29 13:04
阅读 3450·2019-08-29 12:52
阅读 3080·2019-08-23 16:14