摘要:知識(shí)點(diǎn)總結(jié)容器知識(shí)點(diǎn)總結(jié)容器底層實(shí)現(xiàn)是雙向循環(huán)鏈表,所以增刪元素效率高,查詢慢。若只對(duì)單條數(shù)據(jù)插入或刪除,的速度反而優(yōu)于。但若是批量隨機(jī)的插入刪除數(shù)據(jù),的速度大大優(yōu)于因?yàn)槊坎迦胍粭l數(shù)據(jù),要移動(dòng)插入點(diǎn)及之后的所有數(shù)據(jù)。
Java知識(shí)點(diǎn)總結(jié)(Java容器-LinkedList)
@(Java知識(shí)點(diǎn)總結(jié))[Java, Java容器, JavaCollection, JavaList]
LinkedList底層實(shí)現(xiàn)是雙向循環(huán)鏈表,所以增刪元素效率高,查詢慢。線程安全
ArrayList和LinkedList比較
ArrayList是實(shí)現(xiàn)了基于動(dòng)態(tài)數(shù)組的數(shù)據(jù)結(jié)構(gòu),LinkedList基于鏈表的數(shù)據(jù)結(jié)構(gòu)
對(duì)于水機(jī)訪問get和set,ArrayList覺得優(yōu)于LinkedList,因?yàn)長inkedList要移動(dòng)指針
對(duì)于新增和刪除操作add和remove,LinkedList比較占優(yōu)勢(shì),因?yàn)锳rrayList要移動(dòng)數(shù)據(jù)
這一點(diǎn)要看實(shí)際情況的。若只對(duì)單條數(shù)據(jù)插入或刪除,ArrayList的速度反而優(yōu)于LinkedList。但若是批量隨機(jī)的插入刪除數(shù)據(jù),LinkedList的速度大大優(yōu)于ArrayList. 因?yàn)锳rrayList每插入一條數(shù)據(jù),要移動(dòng)插入點(diǎn)及之后的所有數(shù)據(jù)。
手寫LinkedListpublic class MyLinkedList /*implements List*/ { private Node first; private Node last; private int size ; //新增元素 public void add(Object obj) { if (first == null) { Node n = new Node(null,obj,null); first = n; last = n; }else { //直接往last后增加新的節(jié)點(diǎn) Node n = new Node(last, obj, null); last.setNext(n); last = n; } size++; } //獲取元素 public Object get(int index) throws Exception { return getNode(index).getObj(); } //刪除元素 public boolean remove(int index) throws Exception { boolean flag = false; Node temp = getNode(index); if (temp!=null) { temp.getPreview().setNext(temp.getNext()); temp.getNext().setPreview(temp.getPreview()); size--; flag = true; } return flag; } //指定位置加入元素 public boolean add(int index,Object obj) throws Exception { boolean flag = false; Node temp = getNode(index); if (temp!=null) { if (index == 0) { Node n = new Node(null,obj,temp); first = n; }else { Node n = new Node(temp.getPreview(),obj,temp); temp.getPreview().setNext(n); temp.setPreview(n); } size++; flag = true; } return flag; } //替換節(jié)點(diǎn) public boolean set(int index,Object obj) throws Exception{ boolean flag = false; Node temp = getNode(index); System.out.println("獲取的節(jié)點(diǎn)" +temp.getObj()+temp.getPreview().getObj()+temp.getNext().getObj()); if (temp!=null) { Node n = new Node(temp.getPreview(),obj,temp.getNext()); temp.getPreview().setNext(n); temp.getNext().setPreview(n); flag = true; } return flag; } public int size(){ return size; } public void rangeCheck(int index) throws Exception { if (index <0 || index > size) { throw new Exception(); } } public Node getNode(int index) throws Exception { rangeCheck(index); Node temp = null; //if (first != null) { /*for (int i = 0; i < index; i++) { temp = temp.getNext(); }*/ /*int i = 0; while(temp.getNext() != null && i > 1)) {//向右移動(dòng)一位,相當(dāng)于/2 temp = first; for (int i = 0; i < index; i++) temp = temp.getNext(); return temp; } else { temp = last; for (int i = size - 1; i > index; i--) temp = temp.getPreview(); return temp; } } public static void main(String[] args) throws Exception { MyLinkedList list = new MyLinkedList(); list.add("a" ); list.add("b" ); list.add("c" ); list.add("d" ); list.add("e" ); list.add("f" ); System.out.println(list.size()); list.remove(3); list.add(2, "新節(jié)點(diǎn)" ); list.set(4, "替換"); for (int i = 0; i
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/69522.html
摘要:知識(shí)點(diǎn)總結(jié)容器知識(shí)點(diǎn)總結(jié)容器接口與是在同一級(jí)別,都是繼承了接口。另一種隊(duì)列則是雙端隊(duì)列,支持在頭尾兩端插入和移除元素,主要包括。一個(gè)由鏈表結(jié)構(gòu)組成的無界阻塞隊(duì)列。是一個(gè)阻塞的線程安全的隊(duì)列,底層實(shí)現(xiàn)也是使用鏈?zhǔn)浇Y(jié)構(gòu)。 Java知識(shí)點(diǎn)總結(jié)(Java容器-Queue) @(Java知識(shí)點(diǎn)總結(jié))[Java, Java容器] Queue Queue接口與List、Set是在同一級(jí)別,都是繼承了...
摘要:而在集合中,值僅僅是一個(gè)對(duì)象罷了該對(duì)象對(duì)本身而言是無用的。將這篇文章作為集合的總結(jié)篇,但覺得沒什么好寫就回答一些面試題去了,找了一會(huì)面試題又覺得不夠系統(tǒng)。 前言 聲明,本文用的是jdk1.8 花了一個(gè)星期,把Java容器核心的知識(shí)過了一遍,感覺集合已經(jīng)無所畏懼了?。?哈哈哈....),現(xiàn)在來總結(jié)一下吧~~ 回顧目錄: Collection總覽 List集合就這么簡單【源碼剖析】 Ma...
摘要:知識(shí)點(diǎn)總結(jié)容器知識(shí)點(diǎn)總結(jié)容器接口為直接接口。對(duì)于而已,我們一般都是避免使用將當(dāng)做首選,畢竟對(duì)于集合元素而已我們都是進(jìn)行遍歷,只有當(dāng)程序的性能因?yàn)榈念l繁插入和刪除而降低時(shí),再考慮 Java知識(shí)點(diǎn)總結(jié)(Java容器-List) @(Java知識(shí)點(diǎn)總結(jié))[Java, Java容器, JavaCollection, JavaList] [toc] List List接口為Collection直...
摘要:底層使用的是雙向鏈表數(shù)據(jù)結(jié)構(gòu)之前為循環(huán)鏈表,取消了循環(huán)??焖匐S機(jī)訪問就是通過元素的序號(hào)快速獲取元素對(duì)象對(duì)應(yīng)于方法。而接口就是用來標(biāo)識(shí)該類支持快速隨機(jī)訪問。僅僅是起標(biāo)識(shí)作用。,中文名為雙端隊(duì)列。不同的是,是線程安全的,內(nèi)部使用了進(jìn)行同步。 前言 學(xué)習(xí)情況記錄 時(shí)間:week 2 SMART子目標(biāo) :Java 容器 記錄在學(xué)習(xí)Java容器 知識(shí)點(diǎn)中,關(guān)于List的需要重點(diǎn)記錄的知識(shí)點(diǎn)。...
摘要:和的區(qū)別是非線程安全的,效率高是基于線程安全的,效率低底層基于鏈表實(shí)現(xiàn),鏈表內(nèi)存是散亂的,每一個(gè)元素存儲(chǔ)本身內(nèi)存地址的同時(shí)還存儲(chǔ)下一個(gè)元素的地址?;旧隙际且詾榛A(chǔ)。 什么是集合 Java是一門面向?qū)ο蟮恼Z言. 為了方便操作多個(gè)對(duì)象,那么我們就得把這多個(gè)對(duì)象存儲(chǔ)起來 想要存儲(chǔ)多個(gè)對(duì)象(變量),我們就需要一個(gè)容器 集合就是一個(gè)放數(shù)據(jù)的容器(集合類存放的都是對(duì)象的引用,而非對(duì)象本身) ...
閱讀 3340·2021-11-02 14:44
閱讀 3782·2021-09-02 15:41
閱讀 1785·2019-08-29 16:57
閱讀 1852·2019-08-26 13:38
閱讀 3367·2019-08-23 18:13
閱讀 2164·2019-08-23 15:41
閱讀 1728·2019-08-23 14:24
閱讀 3091·2019-08-23 14:03