千鋒教育Java入門全套視頻教程(java核心技術(shù),適合java零基礎(chǔ),Java

LinkedList的add與remov底層筆記::
LinkedList:增源碼(add)
??public boolean add(E e) {
????linkLast(e);//創(chuàng)建一個集合
????return true;
??}
??void linkLast(E e) {
????final Node<E> l = last; //l = 最后一個元素
????final Node<E> newNode = new Node<>(l, e, null);//創(chuàng)建一個元素node
????last = newNode;//將last 指向這個元素
????if (l == null)//判斷有沒有l(wèi)ast
??????first = newNode;//沒有最后一個元素就把first指向剛創(chuàng)建的元素
????else
??????l.next = newNode;//有就把上一個的next指向new
????size++;
????modCount++;
??}
LinkedList:刪源碼(remov)
??public E remove() {
????return removeFirst(); //調(diào)用removeFirst
??}
??public E removeFirst() {
????final Node<E> f = first;
????if (f == null)//判斷要刪除的這個集合是不是空的,空的就報錯
??????throw new NoSuchElementException();
????return unlinkFirst(f);//調(diào)用unlinkFirst
??}
??private E unlinkFirst(Node<E> f) {
????// assert f == first && f != null;
????final E element = f.item; //把元素獲取出來 返回去
????final Node<E> next = f.next;//next指向下一個元素
????f.item = null;//把元素置空
????f.next = null; // help GC //幫助垃圾回收器
????first = next;//把first 指向下一個元素
????if (next == null) //判斷下一個是否為空
??????last = null;//空的就last也是空
????else
??????next.prev = null;// 不是空的就把下一個元素的 prev值為空 (首個元素沒有上一個)
????size--;
????modCount++;
????return element;
??}