LinkedList的add(Object obj)和remove(Object obj)和remove(index)和get(index)的源碼分析

1.add(Object obj)添加元素,底層結構爲鏈表

//構造器
public LinkedList() {

}

		linkedList.add(1);
		linkedList.add(2);
		linkedList.add(3);
		linkedList.add(4);
		linkedList.add(5);
		linkedList.add(6);
		linkedList.add(7);
		linkedList.add(8);
		linkedList.add(9);
		linkedList.add(10);
		linkedList.add(11);

//add(Object obj)

	//把int型的值裝箱
	public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }


	 public boolean add(E e) {
        linkLast(e);
        return true;
    }

	void linkLast(E e) {
		//transient Node<E> last;
        final Node<E> l = last;
		//三個參數的構造器,創建一個新的節點
        final Node<E> newNode = new Node<>(l, e, null);

		//把新的節點賦給last
        last = newNode;

		//此時l爲null
		//所以新的節點賦給first
		// transient Node<E> first;
        if (l == null)
            first = newNode;
        else
            l.next = newNode;
		//transient int size = 0;
        size++;//記錄LinkedList的元素個數
        modCount++;//計數器
    }

	//定義一個節點類
	 private static class Node<E> {
        E item;//對象item
        Node<E> next;//指向想一個節點
        Node<E> prev;//保存上一個節點的內容

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }


2.get(index) 得到index位置上的元素

 public E get(int index) {
		 //檢查下標是否越界
        checkElementIndex(index);
		//返回下標爲index的節點的item,就是要找的元素
        return node(index).item;
    }
	

    private void checkElementIndex(int index) {
        if (!isElementIndex(index))
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

	 Node<E> node(int index) {
       
		//分爲兩部分,這樣能減少時間複雜度
		//如果要刪除的節點的位置在鏈表數量一半的上半部分
        if (index < (size >> 1)) {
			//transient Node<E> first;
            Node<E> x = first;
            for (int i = 0; i < index; i++)
                x = x.next;
			//遍歷找出index位置上的節點,並返回該節點
            return x;
        } else {
            Node<E> x = last;
            for (int i = size - 1; i > index; i--)
                x = x.prev;
			//遍歷找出index位置上的節點,並返回該節點
            return x;
        }
    }


3.remove(index),返回值爲Object類型

	public E remove(int index) {
			//檢查下標index是否越界
			checkElementIndex(index);
			//刪除下標爲index的節點
			return unlink(node(index));
		}

		private void checkElementIndex(int index) {
        if (!isElementIndex(index))
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
	   }

		
	    Node<E> node(int index) {
       
		//分爲兩部分,這樣能減少時間複雜度
		//如果要刪除的節點的位置在鏈表數量一半的上半部分
        if (index < (size >> 1)) {
			//transient Node<E> first;
            Node<E> x = first;
            for (int i = 0; i < index; i++)
                x = x.next;
			//遍歷找出index位置上的節點,並返回該節點
            return x;
        } else {
            Node<E> x = last;
            for (int i = size - 1; i > index; i--)
                x = x.prev;
			//遍歷找出index位置上的節點,並返回該節點
            return x;
        }
    }

	//刪除該節點
	 E unlink(Node<E> x) {
		// assert x != null;
		final E element = x.item;
		final Node<E> next = x.next;
		final Node<E> prev = x.prev;
		
		//如果x節點的prev值爲空,說明這個元素是第一個元素
		//
		if (prev == null) {
			first = next;//把next賦給第一個元素
		} else {
			prev.next = next;//把如果不是第一個元素,這把這個元素的前一個元素賦給這個元素的prev
			x.prev = null;
		}

		//如果x節點的next值爲空,說明這個元素是最後一個元素
		if (next == null) {
			last = prev;//把prev賦給最後一個元素
		} else {
			next.prev = prev;//把如果不是最後一個元素,這把這個元素的前一個元素賦給這個元素的prev
			x.next = null;
		}
		
		x.item = null;//讓其節點的item值爲空
		size--;//零LinkedList的長度-1
		modCount++;
		return element;//返回這個節點
	}



4.1.remove(Object obj),當遇到要刪除的對象爲int類型時,把對象拆箱

public Integer(int value) {
        this.value = value;
    }

	//
	 public boolean remove(Object o) {
		 //如果對象爲null值
        if (o == null) {
			//從第一個節點開始,逐一往後遍歷,知道x節點爲空
            for (Node<E> x = first; x != null; x = x.next) {
				//如果節點的item值爲null值,調用unlink(x)方法
                if (x.item == null) {
                    unlink(x);
                    return true;
                }
            }
        } else {
			//遍歷這個鏈表,找出鏈表中的元素的item值相同的,刪除掉
            for (Node<E> x = first; x != null; x = x.next) {
                if (o.equals(x.item)) {
					//找到想對象的元素,刪除掉
                    unlink(x);
                    return true;
                }
            }
        }
		//如果沒有該元素,則返回false
        return false;
    }

	//刪除該節點
	 E unlink(Node<E> x) {
			// assert x != null;
			final E element = x.item;
			final Node<E> next = x.next;
			final Node<E> prev = x.prev;
			
			//如果x節點的prev值爲空,說明這個元素是第一個元素
			//
			if (prev == null) {
				first = next;//把next賦給第一個元素
			} else {
				prev.next = next;//把如果不是第一個元素,這把這個元素的前一個元素賦給這個元素的prev
				x.prev = null;
			}

			//如果x節點的next值爲空,說明這個元素是最後一個元素
			if (next == null) {
				last = prev;//把prev賦給最後一個元素
			} else {
				next.prev = prev;//把如果不是最後一個元素,這把這個元素的前一個元素賦給這個元素的prev
				x.next = null;
			}
			
			x.item = null;//讓其節點的item值爲空
			size--;//零LinkedList的長度-1
			modCount++;
			return element;//返回這個節點
		}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章