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