Java記錄 -46- ArrayList源碼剖析

來源:互聯網
上載者:User

Java記錄 -46- ArrayList源碼剖析
在編程中經常會使用到集合,而集合中最常用的是ArrayList。集合中存放的依然是對象的引用,而不是對象本身,和數組一樣。當我們學習了數組又學習集合時,發現集合很神奇。數組需要在定義的時候聲明大小,而ArrayList不用管大小,定義了以後可以隨便使用。 查看ArrayList的原始碼,可以發現,ArrayList底層是用數組進行存放元素的。ArrayList並不神奇,底層實現是通過一個對象數組Object[]來存放元素,由於是對象數組,所以只能存放對象,並可以存放任何對象。而數組存放了對象的引用,所以ArrayList也存放對象的引用。 ArrayList的兩個建構函式:我們通常使用ArrayList時會使用其不帶參數的建構函式,或指定一個容量的參數的建構函式,而其底層實現是不帶參數的建構函式會調用帶參數的建構函式,只是不帶參數的建構函式會預設使用傳遞一個10參數,帶參數的建構函式中會聲明一個長度為10的Object數組。 

   /**     * The array buffer into which the elements of the ArrayList are stored.     * The capacity of the ArrayList is the length of this array buffer.     */   private transient Object[] elementData;       public ArrayList(int initialCapacity) {        super();        if (initialCapacity < 0)            throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);        this.elementData = new Object[initialCapacity];    }    /**     * Constructs an empty list with an initial capacity of ten.     */    public ArrayList() {        this(10);    }

 

  下面是我們經常使用的add方法,該方法首先會判斷底層數組的長度是否大於需要的長度,讓將元素放入底層數組中。 
    /**     * 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) {        ensureCapacity(size + 1);  // Increments modCount!!        elementData[size++] = e;        return true;    }

 

 ArrayList的size()方法返回的是集合的大小,其實是底層數組的長度。 ArraList中一個很重要的方法(ArrayList底層是使用數組實現的,為什麼它可以無限添加元素呢?)下面這個方法ensureCapacity做了實現,minCapacity是要追加元素的位置,它和底層數組的大小做比較,如果小於底層數組的長度則什麼也不做;重點在大於底層數組長度時,它做了一些事情:將原數組的大小進行擴容,擴多大呢,原數組的3/2+1;然後使用Arrays.copyOf方法,將原數組的元素拷貝到新指定大小的數組裡,至此實現數組的擴容。 
   /**     * Increases the capacity of this <tt>ArrayList</tt> instance, if     * necessary, to ensure that it can hold at least the number of elements     * specified by the minimum capacity argument.     *     * @param   minCapacity   the desired minimum capacity     */    public void ensureCapacity(int minCapacity) {        modCount++;        int oldCapacity = elementData.length;        if (minCapacity > oldCapacity) {            Object oldData[] = elementData;            int newCapacity = (oldCapacity * 3)/2 + 1;            if (newCapacity < minCapacity)                newCapacity = minCapacity;            // minCapacity is usually close to size, so this is a win:            elementData = Arrays.copyOf(elementData, newCapacity);        }    }

 

上一篇:http://www.bkjia.com/kf/201510/447439.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.