ArrayList source code Interpretation of Java collection

Source: Internet
Author: User

From: jdk1.8.0_121
ArrayListInherit from AbstractList , realize,,, List RandomAccess Cloneable Serializable .

Variable
    // 默认容量为10    privatestaticfinalint10;    // 空数组    privatestaticfinal Object[] EMPTY_ELEMENTDATA = {};    // 默认空数组    privatestaticfinal Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};    // 存放数据的数组,被transient修饰的参数不会被序列化    transient Object[] elementData;    // 实际元素的大小    privateint size;
Construction method
     PublicArrayList (intinitialcapacity) {if(Initialcapacity >0) { This.Elementdata=NewObject[initialcapacity]; }Else if(Initialcapacity = =0) { This.Elementdata= Empty_elementdata; }Else{Throw NewIllegalArgumentException ("Illegal capacity:"+ initialcapacity); }    } PublicArrayList () { This.Elementdata= Defaultcapacity_empty_elementdata; } PublicArrayList (collection<?extendsE> 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; }    }
ToArray () Differences in implementation methods

Note: The ArrayList(Collection<? extends E> c) reason for determining whether to be an object type is because toArray() the calling method is implemented in a different way.

Injava.util.ArrayListIntoArray()The returned object is an array of objects.
    publictoArray() {        return Arrays.copyOf(elementData, size);    }
Injava.util.Arrays, there is a name ofArrayListThe inner class, when calledArrays.asList(), the return isjava.util.Arrays$ArrayListInner class object, notjava.util.ArrayList。java.util.Arrays$ArrayListIn thetoArray()The returned object is an array of objects, but its true type is a generic array.
    privatefinal E[] a;    publictoArray() {        return a.clone();    }
ArrayList expansion
     Public Boolean Add(e) {ensurecapacityinternal(Size +1);//increments modcount!!elementdata[size++] = e;return true; }Private void ensurecapacityinternal(intmincapacity) {if(Elementdata = = Defaultcapacity_empty_elementdata) {mincapacity = Math.Max(Default_capacity, mincapacity); }ensureexplicitcapacity(mincapacity); }Private void ensureexplicitcapacity(intmincapacity) {modcount++;//Overflow-conscious code        if(Mincapacity-elementdata.length>0)Grow(mincapacity); }Private void Grow(intmincapacity) {//Overflow-conscious code        intOldcapacity = Elementdata.length;intNewcapacity = oldcapacity + (oldcapacity >>1);if(Newcapacity-mincapacity <0) newcapacity = mincapacity;if(Newcapacity-max_array_size >0) Newcapacity =hugecapacity(mincapacity);//mincapacity is usually close to size, so this is a win:Elementdata = Arrays.copyOf(Elementdata, newcapacity); }

From the source can be seen, if 1.5 times times elementData.length less than 10, will be elementData the size of the default 10, conversely, will be 1.5 times times the elementData.length expansion.

Set the Elementdata to actual capacity and the dynamically expanded excess capacity will be deleted
    publicvoidtrimToSize() {        modCount++;        if (size < elementData.length) {            0)              ? EMPTY_ELEMENTDATA              : Arrays.copyOf(elementData, size);        }    }

Java Collection ArrayList source code interpretation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.