列表和鏈表有什麼區別,查詢,插入,刪除上的效率有何區別?

List 列表
ArrayList

//實現了list ,底層是動態的對象數組

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
    private static final long serialVersionUID = 8683452581122892189L;

    /**
     * Default initial capacity.
     */
    private static final int DEFAULT_CAPACITY = 10;

    /**
     * Shared empty array instance used for empty instances.
     */
    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 = {};

    /**
     * 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.
     */
    transient Object[] elementData; // non-private to simplify nested class access

LinkedList

//實現了List和Deque雙向列表,底層是鏈表
public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable

ArrayList和LinkedList都實現了List接口 所以是有序的可以重複的集合,但是LinkedList還實現了Deque結構是雙向列表(鏈表式結構).

關係圖
在這裏插入圖片描述
比如 我們修改插入 ‘2’ 到下標爲1的ArrayList,那麼 00002地址給了’2’, 那麼之前在00002的數值需要存到後一個00003地址,
後面的內存地址都往後移動.
然而,LinkedList他是鏈表結構,只記住下標就可以了,當你插入一個值(節點)的時候,它的地址是重新分配的和前後內存地址無關的,改變的只是下標而已. 比如我們也插入’2’ 到下標爲2的LinkedList裏,系統會給它分配一個未使用的地址(假設是0
110),那麼之前下標2以及之後的數據,內存還是之前的地址,修改的僅僅是下標,所以效率高.
在這裏插入圖片描述

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