LinkedList簡介

LinkedList簡介

  1. LinkedList基于雙向鏈表實(shí)現(xiàn)
  2. LinkedList相對(duì)于Arraylist來說,get和set等隨機(jī)訪問會(huì)比較慢,LinkedList需要移動(dòng)指針;add和remove會(huì)比較快。
  3. LinkedList類還為在列表開頭和結(jié)尾的get,remove,insert元素提供統(tǒng)一的命名方法,這些操作允許將鏈表用做堆棧、隊(duì)列或者雙端隊(duì)列。此類實(shí)現(xiàn) Deque 接口,為 add、poll 提供先進(jìn)先出隊(duì)列操作,以及其他堆棧和雙端隊(duì)列操作。
  4. 非線程安全,不同步。

定義

LinkedList集成AbstractSequentialList,實(shí)現(xiàn)了List,Deque,Cloneable,Serializable接口。AbstractSequentialList提供了骨干實(shí)現(xiàn)。Deque一個(gè)線性 collection,支持在兩端插入和移除元素,定義了雙端隊(duì)列的操作。

源碼分析

jdk1.7.0_71

//節(jié)點(diǎn)個(gè)數(shù)
transient int size = 0;
//前驅(qū)節(jié)點(diǎn)
transient Node<E> first;
//后繼節(jié)點(diǎn)
transient Node<E> last;

無參構(gòu)造

public LinkedList() {}

根據(jù)其他容器進(jìn)行構(gòu)造

public LinkedList(Collection<? extends E> c) {}

getFirst()/getLast() 獲取第一個(gè)/獲取最后一個(gè)

public E getFirst() {}
public E getLast() {}

removeFirst()/removeLast() 刪除第一個(gè)/刪除最后一個(gè)

public E removeFirst() {}
public E removeLast() {}

addFirst(E e)/addLast(E e) 添加到頭/尾

public void addFirst(E e) {}
public void addLast(E e) {}

是否包含指定的元素

public boolean contains(Object o) {
    return indexOf(o) != -1;
}

指定元素的位置索引

public int indexOf(Object o) {}

指定元素的最后的位置索引

public int lastIndexOf(Object o) {}

list的大小

public int size() {
        return size;
}

add() 添加到末尾

public boolean add(E e) {}

remove(Object o)/removeFirstOccurrence(Object o) 移除第一個(gè)o

public boolean remove(Object o) {}

public boolean removeFirstOccurrence(Object o){}

addAll(Collection<? extends E> c) 添加到list結(jié)尾

public boolean addAll(Collection<? extends E> c) {}

addAll(int index, Collection<? extends E> c) 指定的位置以后添加全部

public boolean addAll(int index, Collection<? extends E> c) {}

clear() 清空

public void clear() {}

get(int index) 獲取指定位置的元素

public E get(int index) {}

指定位置替換成新元素,返回舊元素

public E set(int index, E element) {}

add(int index, E element)指定位置插入指定元素

public void add(int index, E element) {}

remove(int index) 移除指定位置的元素

public E remove(int index) {}

peek()/peekFirst() 獲取list頭

public E peek() {}

public E peekFirst() {}

element() 獲取list頭

public E element() {}

poll()/pollFirst() 獲取list頭,并刪除

public E poll() {}

public E pollFirst() {}

remove() 刪除第一個(gè)

public E remove() {}

offer(E e)/offerLast(E e) 添加e到尾部

public boolean offer(E e) {}

public boolean offerLast(E e) {}

offerFirst(E e)/push(E e) 添加e到頭部

public boolean offerFirst(E e) {}

public void push(E e){}

peekLast() 獲取list尾

public E peekLast() {}

pollLast() 獲取list尾,并刪除

public E pollLast

pop() 刪除頭

public E pop(){}

removeLastOccurrence(Object o)從后面開始刪除第一個(gè)匹配的元素

public boolean removeLastOccurrence(Object o) {}

listIterator(int index)從指定的位置開始返回一個(gè)listIterator

public ListIterator<E> listIterator(int index) {}

descendingIterator() 逆序迭代器

public Iterator<E> descendingIterator() {
        //內(nèi)部類
        return new DescendingIterator();
    }

clone() 淺拷貝

public Object clone() {}

toArray() 轉(zhuǎn)換成Object數(shù)組

public Object[] toArray() {}

toArray(T[] a) 轉(zhuǎn)換成指定類型的數(shù)組

public <T> T[] toArray(T[] a) {
        if (a.length < size)
            a = (T[])java.lang.reflect.Array.newInstance(
                                a.getClass().getComponentType(), size);
        int i = 0;
        Object[] result = a;
        for (Node<E> x = first; x != null; x = x.next)
            result[i++] = x.item;

        if (a.length > size)
            a[size] = null;

        return a;
    }

參考

https://www.zybuluo.com/pastqing/note/208830

http://blog.csdn.net/u013256816/article/details/50916689

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容