Java ArrayList add element parsing under JDK 1.8

Source: Internet
Author: User
Tags array length

Reprint Please specify http://www.cnblogs.com/majianming/p/8006452.html

I was asked how the Java ArrayList bottom layer was implemented. I answered the array, and he asked me again, how did it implement the addition of the array? I do not know, I dare not answer.

Come back and look at the Java implementation, it is clear that the ArrayList layer is actually implemented with arrays, but how to achieve the expansion of the array?

Simply put, create a new array that is larger than the original, copy all the elements from the original to the new array, and then add the new array in

Starting with the parameterless constructor, the parameterless constructor creates an empty array with a capacity of 0 ( Note that in the case of jdk1.8, the creation of an empty array, or the creation of an array with a capacity of 10 in 1.7, the source is shown in Appendix 1, 2, and 1.8 for example )

You can then start calling the Add method and adding the element, with the public boolean add (E E) method as an example

    1. Calculate the minimum capacity: Judging is not the first time to add, accurately speaking is not an empty array. If it is an empty array, then set the initialization size (that is, the minimum capacity) to 10, if not, then size +1 on the original base.
    2. Determine if you need to expand: determine whether the minimum capacity is greater than the array length (at least 1), and if so, proceed to step 3rd. Why is this judgment? is the minimum capacity of the array can be stored in the number of elements, and the array length is actually the number of storage, then the array is not full of cases, such as the second time when adding, the array capacity is 10 (the first time the initialization is set to 10), the element plus a new increase of only two, is not required 3rd, 4 steps
    3. Expansion calculation:
      1. Get the number of existing elements and prepare to set the new capacity to 1.5 times times the number of original elements
      2. The new capacity and the minimum capacity in 1 are relatively large (so 1.5 times times the original element at initialization is 0 less than the minimum capacity of 10, set to 10)
      3. Compare the results of 2 with integer.max_value-8 (why -8, Doc is to illustrate some of the virtual machine implementation needs to save some more header information, in order to prevent oom, others do not explain, on the StackOverflow to save the maximum size of the array 2, 147,483,648 so the need (see Appendix 3) is also large, there is no way, can only be set to Integer.max_value. In other words, the maximum capacity of the array is reduced by 8, otherwise the maximum value is the largest. This time can not let go then only to throw the exception!
    4. Copy the old element to the new array: The previous step evaluates to just one value, no modification to the original, and no new array created. Next, use arrays.copyof to copy all the elements of the original array to the new array (created at minimum capacity) and return
    5. Adds the added element to the newly returned array finally, length +1

Appendix

    1. JDK 1.7 ArrayList
    2. JDK 1.8 ArrayList
    3. Why-8

Reference

Analysis of ArrayList source code in Java

Set

jdk:1.8_102

Reprint Please specify http://www.cnblogs.com/majianming/p/8006452.html

Java ArrayList add element parsing under JDK 1.8

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.