ArrayList How to increase the size

Source: Internet
Author: User

JDK1.8

Private Static Final object[] Defaultcapacity_empty_elementdata = {}; transient   object[] Elementdata; public ArrayList () { this. Elementdata= defaultcapacity_empty_ Elementdata; }
1. The list object of the ArrayList is essentially stored in a reference array, and some people think that the array has an "autogrow mechanism" that automatically changes the size. Formally, the array is immutable.
Size, actually it just changes the point of the reference array. Now, let's look at how Java implements the ArrayList class.

2.
Second, instantiate the Elementdata array after specifying the initial capacity (capacity) or converting the specified collection to a reference array, or, if not specified, a preset initial capacity of 10
instantiated. Pre-instantiating a private array, and then overwriting the original array through the Copyof method, is the key to implementing an automatic change in the size of the ArrayList (size). Some say ArrayList is a complex array, and I
Think ArrayList is a combination of methods about arrays of systems.

3.
Capacity is the size of the allocation, and size is the actual occupancy size.
    Private Static Final int Default_capacity = 10;

Private voidGrowintmincapacity) {        //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); }

ArrayList How to increase the size

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.