面試Java基礎之ArrayList和LinkedList的區別

前言

      我們在面試的時候,經常遇到面試官問的Java基礎問題,今天記錄一個經常被問到的問題————ArrayList和LinkedList的區別。

二者區別

     我們先說結論,然後再從源碼角度去看具體實現。

  1. 前者底層是動態數組實現;後者底層是鏈表實現。
  2. 隨機訪問數據:前者快,後者慢。
  3. 插入和刪除(非末尾)數據:前者慢,後者快。
  4. 前者需要擴容;後者不需要擴容。

源碼分析

分析的源碼是基於jdk1.8的

ArrayList

從經常使用的方法(比如構造方法、add、get、remove)入手開始分析。
構造方法如下:

/**
 * The array buffer into which the elements of the ArrayList are stored.
 * The capacity of the ArrayList is the length of this array buffer. Any
 * empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA
 * will be expanded to DEFAULT_CAPACITY when the first element is added.
 * 
 * 用elementData數組來存儲ArrayList中的數據,ArrayList的長度就是數組長度,當第一個元素被add進來之後,數組的長度就被擴展爲DEFAULT_CAPACITY=10
 */
transient Object[] elementData; // non-private to simplify nested class access


/**
 * Shared empty array instance used for empty instances.
 * 如果創建的是空list、則使用EMPTY_ELEMENTDATA數組、所有的空list指向此數組、避免代碼中過多的空數組造成資源浪費
 */
private static final Object[] EMPTY_ELEMENTDATA = {};

/**
 * Shared empty array instance used for default sized empty instances. We
 * distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when
 * first element is added.
 */
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};

/**
 * Constructs an empty list with the specified initial capacity.
 *
 * @param  initialCapacity  the initial capacity of the list
 * @throws IllegalArgumentException if the specified initial capacity
 *         is negative
 */
public ArrayList(int initialCapacity) {
    if (initialCapacity > 0) {
        this.elementData = new Object[initialCapacity];
    } else if (initialCapacity == 0) {
    	//空list的時候。使用EMPTY_ELEMENTDATA作爲存儲數據的數組、避免資源浪費
        this.elementData = EMPTY_ELEMENTDATA;
    } else {
        throw new IllegalArgumentException("Illegal Capacity: "+
                                           initialCapacity);
    }
}
/**
 * Constructs an empty list with an initial capacity of ten.
 */
public ArrayList() {
    this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}

/**
 * Constructs a list containing the elements of the specified
 * collection, in the order they are returned by the collection's
 * iterator.
 *
 * @param c the collection whose elements are to be placed into this list
 * @throws NullPointerException if the specified collection is null
 */
public ArrayList(Collection<? extends E> c) {
    elementData = c.toArray();
    if ((size = elementData.length) != 0) {
        // c.toArray might (incorrectly) not return Object[] (see 6260652)
        if (elementData.getClass() != Object[].class)
            elementData = Arrays.copyOf(elementData, size, Object[].class);
    } else {
        // replace with empty array.
        this.elementData = EMPTY_ELEMENTDATA;
    }
}

三個構造方法:
第一個構造方法有一個參數,用來設置初始長度的,如果明確知道list的長度,則使用此方法來構造,若傳進來的參數爲0,則直接使用EMPTY_ELEMENTDATA數組,這樣做的好處,是避免程序中,有過多的空list的情況下,造成資源浪費,jdk1.8以前的版本是,如果傳進來的參數爲0,則直接new一個空數組,這樣會造成資源的浪費;
第二個方法構造方法,也是我們經常用的構造方法,沒有參數的構造方法會使用DEFAULTCAPACITY_EMPTY_ELEMENTDATA數組作爲數據容器;
第三個構造方法是有一個Collection類型參數的構造方法,若參數中的數據不爲空,會把參數中的數據,存進elementData數組中;
下面看看如何add數據的

/**
 * 在list末尾增加一個數據,
 * Appends the specified element to the end of this list.
 *
 * @param e element to be appended to this list
 * @return <tt>true</tt> (as specified by {@link Collection#add})
 */
public boolean add(E e) {
	//擴容
    ensureCapacityInternal(size + 1);  // Increments modCount!!
    elementData[size++] = e;
    return true;
}

private void ensureCapacityInternal(int minCapacity) {
    if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
        minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
    }

    ensureExplicitCapacity(minCapacity);
}

private void ensureExplicitCapacity(int minCapacity) {
    modCount++;

    // overflow-conscious code
    if (minCapacity - elementData.length > 0)
        grow(minCapacity);
}

/**
  * 真正的擴容的方法
  * Increases the capacity to ensure that it can hold at least the
  * number of elements specified by the minimum capacity argument.
  *
  * @param minCapacity the desired minimum capacity
  */
 private void grow(int minCapacity) {
     // overflow-conscious code
     //當前的容量
     int oldCapacity = elementData.length;
     //新容量是當前容量的1.5倍
     int newCapacity = oldCapacity + (oldCapacity >> 1);
	 //如果新容量比所需要的容量小,就使用所需要的容量擴容
     if (newCapacity - minCapacity < 0)
         newCapacity = minCapacity;
     //如果新容量大於MAX_ARRAY_SIZE=Integer.MAX_VALUE - 8;新容量會根據所需容量minCapacity與MAX_ARRAY_SIZE大小,分別使用Integer.MAX_VALUE或者Integer.MAX_VALUE - 8
     if (newCapacity - MAX_ARRAY_SIZE > 0)
         newCapacity = hugeCapacity(minCapacity);
     // minCapacity is usually close to size, so this is a win:
     //將elementData擴容並將elementData中的內容複製到擴容後的數組elementData
     //這裏會調用到native方法
     elementData = Arrays.copyOf(elementData, newCapacity);
 }
 
private static int hugeCapacity(int minCapacity) {
    if (minCapacity < 0) // overflow
        throw new OutOfMemoryError();
    return (minCapacity > MAX_ARRAY_SIZE) ?
        Integer.MAX_VALUE :
        MAX_ARRAY_SIZE;
}

完成以上擴容檢測並完成擴容後,再回到add方法中,將要添加的元素添加到elementData的末尾即可。
然後簡單看一下add(int index, E element)方法:

/**
 * Inserts the specified element at the specified position in this
 * list. Shifts the element currently at that position (if any) and
 * any subsequent elements to the right (adds one to their indices).
 *
 * @param index index at which the specified element is to be inserted
 * @param element element to be inserted
 * @throws IndexOutOfBoundsException {@inheritDoc}
 */
public void add(int index, E element) {
    rangeCheckForAdd(index);

    ensureCapacityInternal(size + 1);  // Increments modCount!!
    System.arraycopy(elementData, index, elementData, index + 1,
                     size - index);
    elementData[index] = element;
    size++;
}

跟add(E element)方法基本一樣,唯一不同的是多了一句System.arraycopy(elementData, index, elementData, index + 1, size - index);這句代碼就是將源數組中從位置index之後的數據整體向後移動一位,以便空出index位,爲插入element做準備、然後將element插入到index位置。
get(int index)方法很簡單不貼代碼了。
看下刪除數據的吧remove(int index)和remove(Object o)兩個方法

/**
 * Removes the element at the specified position in this list.
 * Shifts any subsequent elements to the left (subtracts one from their
 * indices).
 *
 * @param index the index of the element to be removed
 * @return the element that was removed from the list
 * @throws IndexOutOfBoundsException {@inheritDoc}
 */
public E remove(int index) {
    rangeCheck(index);

    modCount++;
    //獲取要刪除的值
    E oldValue = elementData(index);
	//需要移動的元素的個數,被刪除的元素之後的元素都需要前移一位
    int numMoved = size - index - 1;
    if (numMoved > 0)
    	//將被刪的元素之後的元素,向前移動一位
        System.arraycopy(elementData, index+1, elementData, index,
                         numMoved);
     //將最後一位置爲空                    
    elementData[--size] = null; // clear to let GC do its work

    return oldValue;
}

這個很簡單,看註釋就好。

/**
 * Removes the first occurrence of the specified element from this list,
 * if it is present.  If the list does not contain the element, it is
 * unchanged.  More formally, removes the element with the lowest index
 * <tt>i</tt> such that
 * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>
 * (if such an element exists).  Returns <tt>true</tt> if this list
 * contained the specified element (or equivalently, if this list
 * changed as a result of the call).
 *
 * @param o element to be removed from this list, if present
 * @return <tt>true</tt> if this list contained the specified element
 */
public boolean remove(Object o) {
    if (o == null) {
        for (int index = 0; index < size; index++)
            if (elementData[index] == null) {
                fastRemove(index);
                return true;
            }
    } else {
        for (int index = 0; index < size; index++)
            if (o.equals(elementData[index])) {
                fastRemove(index);
                return true;
            }
    }
    return false;
}

很簡單,從0開始遍歷數組,找到第一個==被刪除元素的位置,調用fastRemove(index)方法

/*
 * Private remove method that skips bounds checking and does not
 * return the value removed.
 */
private void fastRemove(int index) {
    modCount++;
    int numMoved = size - index - 1;
    if (numMoved > 0)
        System.arraycopy(elementData, index+1, elementData, index,
                         numMoved);
    elementData[--size] = null; // clear to let GC do its work
}

what?這不就是remove(int index)方法嗎?哈哈。邏輯一樣。到此刪除的邏輯講清楚了
ArrayList的源碼就分析到這裏,其實就是對數組操作的封裝,其他方法,感興趣可以自行查看源碼。

LinkedList

我們也從構造方法,add、get和remove入手,先看構造方法:

/**
 * Constructs an empty list.
 */
public LinkedList() {
}

/**
 * Constructs a list containing the elements of the specified
 * collection, in the order they are returned by the collection's
 * iterator.
 *
 * @param  c the collection whose elements are to be placed into this list
 * @throws NullPointerException if the specified collection is null
 */
public LinkedList(Collection<? extends E> c) {
    this();
    addAll(c);
}

常用的是第一個,構造一個空的list;對於第二個會通過參數來構造一個list,其中調用到了addAll(Collection<? extends E> c)。先不看他,因爲我們還不知道LinkedList是用什麼結構存儲數據的呢,看下LinkedList都有哪些全局變量

transient int size = 0;

/**
 * Pointer to first node.
 * Invariant: (first == null && last == null) ||
 *            (first.prev == null && first.item != null)
 * 指向第一個節點
 */
transient Node<E> first;

/**
 * Pointer to last node.
 * Invariant: (first == null && last == null) ||
 *            (last.next == null && last.item != null)
 * 指向最後一個節點
 */
transient Node<E> last;

三個,第一個不用說,後兩個是一個Node類型。這是什麼類型呢。這是LinkedList的一個內部類:

private static class Node<E> {
    E 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;
    }
}

這不是一個鏈表結構的元素的結構嘛!那LinkedList就是用鏈表存儲數據的。
看下addAll(Collection<? extends E> c)方法最終調用的方法

/**
 * Inserts all of the elements in the specified collection into this
 * list, starting at the specified position.  Shifts the element
 * currently at that position (if any) and any subsequent elements to
 * the right (increases their indices).  The new elements will appear
 * in the list in the order that they are returned by the
 * specified collection's iterator.
 *
 * @param index index at which to insert the first element
 *              from the specified collection
 * @param c collection containing elements to be added to this list
 * @return {@code true} if this list changed as a result of the call
 * @throws IndexOutOfBoundsException {@inheritDoc}
 * @throws NullPointerException if the specified collection is null
 */
public boolean addAll(int index, Collection<? extends E> c) {
    checkPositionIndex(index);

    Object[] a = c.toArray();
    int numNew = a.length;
    if (numNew == 0)
        return false;

    Node<E> pred, succ;//pred代表要插入的Collection的節點的前驅
    //在末尾添加的時候,所以pred指向最後一個節點
    if (index == size) {
        succ = null;
        pred = last;
    } else {//不是在末尾添加的邏輯
    	//先獲取要插入位置上節點
        succ = node(index);
        //然後前驅指向被插入鏈表上插入位置節點的前驅
        pred = succ.prev;
    }
	//遍歷要插入的Collection集合
    for (Object o : a) {
        @SuppressWarnings("unchecked") E e = (E) o;
        //對每一個元素生成一個Node 、pred爲前驅
        Node<E> newNode = new Node<>(pred, e, null);
        if (pred == null)//如果沒有前驅,代表是第一個節點firstNode
            first = newNode;
        else//否則把前一個前驅的後繼指向新生成的節點
            pred.next = newNode;
        //然後將新生成的節點更改爲下一個節點的前驅
        pred = newNode;
    }

    if (succ == null) {//將Collection添加到末尾的情況下,last指向最後一個節點
        last = pred;
    } else {
    	//將Collection中的最後一個節點的next指向原list中index位置上的節點succ
        pred.next = succ;
        //將原list中index位置上的節點succ的前驅指向Collection中的最後一個節點
        succ.prev = pred;
    }
	//更新size
    size += numNew;
    modCount++;
    return true;
}

這裏的邏輯有點繞,看不懂可以在紙上畫一畫,其實就是鏈表插入操作,分爲在末尾插入和中間插入兩種情況,並保存全局變量first和last。
構造方法看完了,接下來看下單個元素的添加add(E e)方法吧

/**
 * Appends the specified element to the end of this list.
 * 在list的末尾添加一個新的元素
 * <p>This method is equivalent to {@link #addLast}.
 *
 * @param e element to be appended to this list
 * @return {@code true} (as specified by {@link Collection#add})
 */
public boolean add(E e) {
    linkLast(e);
    return true;
}

/**
* Links e as last element.
 */
void linkLast(E e) {
	//獲取當前list的最後一個元素
    final Node<E> l = last;
    //生成一個新的元素,並把新元素的前驅指向last,後繼指爲null
    final Node<E> newNode = new Node<>(l, e, null);
    //更新last元素爲新生成的這個node
    last = newNode;
    //last==null表明當前是空表,所以first需要指向新生成的newNode
    if (l == null)
        first = newNode;
    //非空表 原list的last節點的next需要指向新生成的節點newNode
    else
        l.next = newNode;
   	//更新size
    size++;
    modCount++;
}

在看下add(int index , E element)方法,在中間位置插入輸入

/**
 * Inserts the specified element at the specified position in this list.
 * Shifts the element currently at that position (if any) and any
 * subsequent elements to the right (adds one to their indices).
 *
 * @param index index at which the specified element is to be inserted
 * @param element element to be inserted
 * @throws IndexOutOfBoundsException {@inheritDoc}
 */
public void add(int index, E element) {
    checkPositionIndex(index);
	//判斷是否是在末尾插入,若是調用linkLast方法,否則調用linkBefore方法
    if (index == size)
        linkLast(element);
    else
        linkBefore(element, node(index));
}

/**
 * Inserts element e before non-null Node succ.
 * succ 是要插入位置的節點
 * e 是要插入的節點
 */
void linkBefore(E e, Node<E> succ) {
    // assert succ != null;
    //定義一個節點pred保存要插入位置的節點的前驅節點
    final Node<E> pred = succ.prev;
    //新生成一個節點前驅爲上一步保存的pred,後繼爲succ節點
    final Node<E> newNode = new Node<>(pred, e, succ);
    //將succ的前驅指向新生成的節點
    succ.prev = newNode;
    //判斷插入位置是否是第一個位置,若是則滿足pred==null 將first指向newNode
    if (pred == null)
        first = newNode;
    else//否則更新pred節點的後繼爲新生成的節點
        pred.next = newNode;
    //更新size
    size++;
    modCount++;
}

add方法分析完畢,下面看一下get方法

/**
 * Returns the element at the specified position in this list.
 *
 * @param index index of the element to return
 * @return the element at the specified position in this list
 * @throws IndexOutOfBoundsException {@inheritDoc}
 */
public E get(int index) {
    checkElementIndex(index);
    return node(index).item;
}
/**
 * Returns the (non-null) Node at the specified element index.
 */
Node<E> node(int index) {
    // assert isElementIndex(index);
	//判斷index是否小於size的1/2
    if (index < (size >> 1)) {
        Node<E> x = first;
        //從list頭開始遍歷查找數據找到第index位置上的元素
        for (int i = 0; i < index; i++)
            x = x.next;
        return x;
    } else {//否則從list尾開始遍歷,找到index位置上的元素
        Node<E> x = last;
        for (int i = size - 1; i > index; i--)
            x = x.prev;
        return x;
    }
}

get方法需要從list的頭或者尾開始遍歷查找,所以比起數組存儲的ArrayList來說,會比較耗時。

下面來看看remove(int index)方法

/**
 * Removes the element at the specified position in this list.  Shifts any
 * subsequent elements to the left (subtracts one from their indices).
 * Returns the element that was removed from the list.
 *
 * @param index the index of the element to be removed
 * @return the element previously at the specified position
 * @throws IndexOutOfBoundsException {@inheritDoc}
 */
public E remove(int index) {
    checkElementIndex(index);
    //獲取要刪除位置的元素並調用unlink方法
    return unlink(node(index));
}

/**
 * Unlinks non-null node x.
 */
E unlink(Node<E> x) {
    // assert x != null;
    //獲取element及其前驅和後繼
    final E element = x.item;
    final Node<E> next = x.next;
    final Node<E> prev = x.prev;
	
	//前驅爲null表明是第一個元素,更新first爲此node的後繼
    if (prev == null) {
        first = next;
    } else {
    	//否則不是第一個元素,將前驅的後繼指向當前node的後繼
        prev.next = next;
        //斷開當前node的前驅
        x.prev = null;
    }
	//後繼爲null表明是最後一個元素,更新last指此向node的前驅
    if (next == null) {
        last = prev;
    } else {
    	//否則不是最後一個元素,將後繼的前驅指向盪鞦韆node的前驅
        next.prev = prev;
        //斷開當前node的後繼
        x.next = null;
    }
	//產出node的item並更新size
    x.item = null;
    size--;
    modCount++;
    return element;
}

查看源碼remove(Object o)

/**
 * Removes the first occurrence of the specified element from this list,
 * if it is present.  If this list does not contain the element, it is
 * unchanged.  More formally, removes the element with the lowest index
 * {@code i} such that
 * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>
 * (if such an element exists).  Returns {@code true} if this list
 * contained the specified element (or equivalently, if this list
 * changed as a result of the call).
 *
 * @param o element to be removed from this list, if present
 * @return {@code true} if this list contained the specified element
 */
public boolean remove(Object o) {
    if (o == null) {
        for (Node<E> x = first; x != null; x = x.next) {
            if (x.item == null) {
                unlink(x);
                return true;
            }
        }
    } else {
        for (Node<E> x = first; x != null; x = x.next) {
            if (o.equals(x.item)) {
                unlink(x);
                return true;
            }
        }
    }
    return false;
}

根據要刪除的數據是否爲null、分別遍歷查找第一個出現的o,並調用unlink方法。remove方法也講完了。
以上就是經常用到的方法的源碼分析

總結

最後來個總結吧。看前面列出的區別點,之所以出現“隨機訪問數據:前者快,後者慢。”的情況,就是因爲後者的隨機訪問,需要從頭遍歷鏈表,因爲他們存儲的位置是不連續的。而對於“插入和刪除(非末尾)數據:前者慢,後者快。”這個問題,由於數組存儲位置是連續的,從中間刪了的數據,空出來的位置,需要由後面的元素補上空位,需要移動元素,而後者不需要。所以會有此差異。

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