ArrayList dynamic expansion mechanism

Initialization: There are three ways
1. The default constructor will be initialized to a default size of the interior of the array: public the ArrayList ();
2. ICollection object with a structure, and the additive element to the set ArrayList: public ArrayList (Collection <? the extends E> c)
3. with the specified size to initialize an array of interior: public ArrayList (int initialCapacity)

Let's focus on what the implementation process of the no-argument constructor:
but its initial capacity through code 0 . But before JDK1,6, the initial capacity of 10.

Expansion conditions occur:
The incoming minCapacity minimum required capacity and the capacity to compare the length of the array, the array if greater than or equal minCapactity capacity is required for expansion. (If the storage array is actually empty array, the minimum required capacity is the default capacity)

to achieve expansion:
JDK7 employed >> bit operation, a right movement. Expanding the capacity equivalent to 1.5 times;
example: add elements to the ArrayList 20
when only the first dispensing element 10 is inserted (default) object space. After the expansion will grow at 1.5 times.
When the addition is the first time data 11, continue Arraylist expansion becomes 10 * 15 = 1.5;
When the addition of 16 data, continued expansion becomes 15 * 22 = 1.5;

Summary:
In JDK1.7, if no parameters configured by the words, 0 is the initial capacity of the array, when the array is actually added, really allocated capacity.
Each at 1.5 times (operation position) by a ratio of expansion copeOf manner.
In JKD1.6, if no parameters configured by the words, the initial capacity of the array 10 after each expansion copeOf by way doubly capacity was 1.5 1. The above principle is the dynamic expansion.

Guess you like

Origin www.cnblogs.com/weigy/p/12573945.html