Automatic expansion mechanism of Java ArrayList __java

Source: Internet
Author: User

Note:

There may be differences in the expansion mechanism for different JDK versions
Experimental environment: JDK1.8

expansion mechanism :

When adding elements to the ArrayList, ArrayList will increase its storage capacity and meet the requirements for storing new elements if it is to meet the storage capabilities of the new elements beyond ArrayList storage of new elements

ArrayList: The essence of data storage through internally maintained array objects

①: Analysis of the ArrayList Add (E) method

Public boolean Add (E e) {
        ensurecapacityinternal (size + 1);  Increments modcount!!
        elementdata[size++] = e;
        return true;
    }

Analysis: The Add method first ensures that the current ArrayList-maintained array has the ability to store new elements through the Ensurecapacityinternal () method, and then stores the elements in the tail of the array elementdata after processing
Elementdata:arraylist the array that is really used to store the elements

②: Analysis of Ensurecapacityinternal methods

private void ensurecapacityinternal (int mincapacity) {
        if (Elementdata = = Defaultcapacity_empty_elementdata) {
            mincapacity = Math.max (default_capacity, mincapacity);
        }
        Ensureexplicitcapacity (mincapacity);
    }

Analysis: Ensurecapacityinternal Determines whether the default element of ArrayList stores the data as null or empty, and sets the minimum required storage capacity as the maximum value between two data that is required to store and the number of default storage elements, Then call the Ensureexplicitcapacity method to achieve this minimum required storage capability

Note: The ArrayList storage space is not required to create a one, but a phased creation, generally reserving storage space.
For example, if ArrayList needs to store 10 elements, just ArrayList can store only 6 elements, the remaining 4 elements cannot be stored, ArrayList may expand 10 elements at once, this ArrayList has 20 elements of storage capacity, within storage capacity , the next time you store the elements, you don't need to expand again.

③: Analysis Ensureexplicitcapacity Method:

private void ensureexplicitcapacity (int mincapacity) {
        modcount++;

        Overflow-conscious Code
        if (Mincapacity-elementdata.length > 0)
            grow (mincapacity);
    }

Analysis: If the minimum required storage capacity >arraylist existing storage capacity, this means that ArrayList storage capacity is insufficient, so call grow ();
④: Analyzing the Grow () method

private void Grow (int mincapacity) {
        //overflow-conscious code
        int oldcapacity = elementdata.length;
        int newcapacity = oldcapacity + (oldcapacity >> 1);
        if (newcapacity-mincapacity < 0)
            newcapacity = mincapacity;
        if (newcapacity-max_array_size > 0)
            newcapacity = hugecapacity (mincapacity);
        Mincapacity is usually close to size, and so it is a win:
        Elementdata = arrays.copyof (Elementdata, newcapacity);
    }

Analysis: When the ArrayList expansion, first set the new storage capacity of 1.5 times times the original

int newcapacity = oldcapacity + (oldcapacity >> 1);

If the expansion is still unable to meet the requirements of the max_array_size comparison, to obtain the maximum value,
If the ability to max_array_size size is still not satisfied, the maximum allowable value of ArrayList can be obtained through the Hugecapacity () method:

private static int hugecapacity (int mincapacity) {
        if (mincapacity < 0)//overflow
            throw new OutOfMemoryError ( );
        Return (Mincapacity > max_array_size)?
            Integer.max_value:
            max_array_size;
    }

From the Hugecapacity method, the maximum storage capacity of ArrayList is as follows: The number of stored elements is the range of integral type.
When you determine the latest number of storage elements after ArrayList expansion, call
Elementdata = arrays.copyof (Elementdata, newcapacity);
To achieve the expansion of elementdata array, the whole process is the ArrayList automatic expansion mechanism work flow

Extended:
The system implementation of ArrayList automatic expansion mechanism

  public static native void Arraycopy
  (object src,  int  srcpos,
   object dest, int destpos,
   int length );

Arraycopy logo as native means that the local library of JDK, will inevitably do IO operations, if the frequent expansion of ArrayList, no doubt will reduce the use of ArrayList performance, so when we determine the number of added elements, We can know in advance and specify the number of ArrayList that can be stored, so that when we add elements to the ArrayList, we can avoid ArrayList automatic expansion, thus improving the performance of ArrayList

ArrayList include parameter constructors: the ability to specify storage elements when initializing:

 public ArrayList (int initialcapacity) {if (initialcapacity > 0) {THIS.E
        Lementdata = new Object[initialcapacity];
        else if (initialcapacity = 0) {this.elementdata = Empty_elementdata;                                               
        else {throw new IllegalArgumentException ("Illegal Capacity:" +initialcapacity); }
    }

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.