Arraylist dynamic expansion

JDK1.7 environment

1, configuration parameters

ArrayList() 

ArrayList (int initialCapacity) with the specified size to initialize internal array

ArrayList (Collection <? Extends E> c) with a construct ICollection object, and the set of elements to ArrayList

2、ArrayList() 

ArrayList list = new ArrayList<String>();

Summary: The initial capacity of the array 10 , each expansion by copeOf manner, after the capacity of the original 1.5 times

 

Source:

ElementData just an empty object array initialized array of objects.

elementData: buffer storage array ArrayList elements. ArrayList capacity is the length of this array buffer. So in this case ArrayList capacity is 0;

 

When performing List.add () method, ArrayList initialization only the size of the array capacity

 

When the first call to add (), elementData == EMPTY_ELEMENTDATA will be equal, and the value is an empty array.

The first call minCapacity = 1,

The value is a value acquired DEFAULT_CAPACITY; minCapacity = Math.max (DEFAULT_CAPACITY, minCapacity)

In ensureExplicitCapacity method, first add () method, this time minCapacity = 10, elementData.length = 1,

So it will not enter the grow () method.

When minCapacity greater than 10, performs Grow () calculated for expansion,

E.g:

When added to the first element of the array 11, the array 15 of expanded capacity. 

int  newCapacity =  10 +  (10 >> 1) = 15

      When added to the first element of the array 16, the array 22 of expanded capacity.

int  newCapacity =  15+  (15>> 1) = 22

 

 

Guess you like

Origin blog.csdn.net/u012965203/article/details/91867567