一、屬性及獲取屬性:
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; }二、構(gòu)造函數(shù)
//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; } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/73424.html
摘要:介紹是線程不安全的,允許元素為的雙向鏈表。構(gòu)造方法共有兩個構(gòu)造方法,一個是創(chuàng)建一個空的構(gòu)造函數(shù),一個是將已有的添加到中。是將元素插入到的頭部。下一篇文章繼續(xù)分析上次分析了的結(jié)構(gòu)和添加方法這次開始分析下面的。注意源碼版本為直接進(jìn)入正題。 如果本文中有不正確的地方請指出由于沒有留言可以在公眾號添加我的好友共同討論。 1.介紹 LinkedList 是線程不安全的,允許元素為null的雙向鏈...
摘要:在次操作中其實即尾節(jié)點是共享資源,當(dāng)多個線程同時執(zhí)行此方法的時候,其實會出現(xiàn)線程安全問題。同樣會出現(xiàn)并發(fā)安全問題,下面對此問題進(jìn)行分析。 1.LinkedList源碼分析 LinkedList的是基于鏈表實現(xiàn)的java集合類,通過index插入到指定位置的時候使用LinkedList效率要比ArrayList高,以下源碼分析是基于JDK1.8. 1.1 類的繼承結(jié)構(gòu) LinkedLis...
摘要:它們會在鏈表為空時,拋出獲取尾節(jié)點數(shù)據(jù)方法兩者區(qū)別方法在鏈表為空時,會拋出,而則不會,只是會返回。 目錄: 0-1. 簡介 0-2. 內(nèi)部結(jié)構(gòu)分析 0-3. LinkedList源碼分析 0-3-1. 構(gòu)造方法 0-3-2. 添加add方法 0-3-3. 根據(jù)位置取數(shù)據(jù)的方法 0-3-4. 根據(jù)對象得到索引的方法 0-3-5. 檢查鏈表是否包含某對象的方法 ...
摘要:表明該類是可以序列化的。與對比并沒有實現(xiàn),而實現(xiàn)表明其支持快速通常是固定時間隨機(jī)訪問。此接口的主要目的是允許一般的算法更改其行為,從而在將其應(yīng)用到隨機(jī)或連續(xù)訪問列表時能提供良好的性能。這是隨機(jī)訪問效率低的原因之一。指定節(jié)點不能為。 總覽 showImg(https://segmentfault.com/img/bVbsIzr?w=1007&h=600); 定義 public class...
摘要:一源碼分析本文分析雙向鏈表的查詢操作源碼實現(xiàn)。中源程序中,的查詢操作,通過函數(shù)實現(xiàn)。源程序中使用循環(huán)進(jìn)行遍歷。表示鏈表元素索引,初值為。針對空元素的情況,用循環(huán)遍歷,查找元素為的節(jié)點,并返回索引。 一、contains源碼分析 本文分析雙向鏈表LinkedList的查詢操作源碼實現(xiàn)。jdk中源程序中,LinkedList的查詢操作,通過contains(Object o)函數(shù)實現(xiàn)。具體...
摘要:源碼分析是一個雙向鏈表的數(shù)據(jù)結(jié)構(gòu)實現(xiàn)。對于支持隨機(jī)訪問數(shù)據(jù)的比如數(shù)組,應(yīng)該優(yōu)先使用。一個有序的集合支持在頭和尾進(jìn)行插入和刪除元素。的大多實現(xiàn)元素數(shù)量是沒有大小限制的。構(gòu)造方法第一個是一個空的構(gòu)造器,第二個構(gòu)造器調(diào)用了方法。 LinkedList源碼分析 LinkedList是一個雙向鏈表的數(shù)據(jù)結(jié)構(gòu)實現(xiàn)。 類的實現(xiàn)接口及繼承父類 public class LinkedList exten...
閱讀 3724·2021-11-23 09:51
閱讀 1388·2021-11-10 14:35
閱讀 4026·2021-09-22 15:01
閱讀 1295·2021-08-19 11:12
閱讀 392·2019-08-30 15:53
閱讀 1705·2019-08-29 13:04
閱讀 3442·2019-08-29 12:52
閱讀 3069·2019-08-23 16:14