laitimes

ArrayList Source Code Brief Introduction Describes Common Methods

author:Alibaba Cloud Yunqi

<h1 class="pgc-h-arrow-right" data-track="1" > preface</h1>

ArrayList, as the most commonly used collection in our development, as a class used very frequently, we may wish to read the source code to talk about it.

<h1 class="pgc-h-arrow-right" data-track="3" > introduction</h1>

ArrayList inheritance is as follows

ArrayList Source Code Brief Introduction Describes Common Methods

AaaryList mainly implements the List interface, while marking it as serializable Serializable, Replicable CloneAble, and Supports Random Access Random Access.

Several important member variables

data structure

ArrayList bottom is an array, the array will expand as the data grows, the expansion of the array is to create a new large capacity array, and then copy the data on the old array into the new array. About the expansion, we will explain in detail later.

Because it is an array, random access is supported and is ordered.

<h1 class="pgc-h-arrow-right" data-track="11" > common method</h1>

ArrayList() parameterless constructor

Initializes the array to an empty array. Distinguish from empty element data to see how much to inflate when adding the first element.

add(E) Adds an element

Appends the specified element to the end of this list

When adding an element, it is first checked to see if it exceeds the capacity, and if it is exceeded, it needs to be resized.

When an element is added for the first time, the size is the default value of 0, and a minimum capacity minCapacity is calculated, if it is created without a parameter structure, the default capacity is 10.

Math .max (DEFAULT_CAPACITY, minCapacity), where the incoming minCapacity is 0, so a larger 10 is obtained.

If the calculated minimum capacity is greater than the original capacity minCapacity - elementData.length &gt; 0, the capacity is expanded.

The scaling algorithm is to scale to 1.5 times the old capacity, and if the rescaled capacity is still less than the minimum capacity minCapacity required, the new capacity takes the minimum capacity.

If the size of the resized capacity exceeds the maximum capacity, the following operations are performed

After calculating the expanded capacity, the capacity is expanded, that is, a new array is initialized to the new capacity, and then the old elements are copied to the new array. elementData = Arrays.copyOf(elementData, newCapacity);

Why can't you modify the list in forEach

ArrayList performs modCount++ when adding new and deleting elements

modCount is defined in The ArrayList's parent class, AbstractList.

Let's then look at the implementation of forEach.

Before traversal, the modCount value will be staged, and each loop will determine whether the modCount has changed, and if it is changed, the loop will jump out of the loop, and then throw an exception.

Original link: http://click.aliyun.com/m/1000302394/

This article is the original content of Alibaba Cloud and may not be reproduced without permission.