[Data structure] - ArrayList exploration

I. Overview

ArrayList can be understood as a dynamic array. Compared with the java array, its capacity can be dynamically longer. ArrayList is the implementation of the variable array of the List interface, allowing all elements including null values. In addition to implementing the List interface, this class also provides methods to manipulate the size of the array used internally to store the list (this class is roughly equivalent to the Vector class, except that this class is not synchronized)

Each ArrayList instance has a capacity, which is the size of the array used to store the list elements. As elements are added to the ArrayList, its capacity also grows automatically. Autogrow results in a new copy of the data to the new array. Therefore, if you can predict the amount of data, you can specify its capacity when constructing the ArrayList. Applications can also use the ensureCapacity operation to increase the capacity of an ArrayList instance before adding a large number of elements, which can reduce the number of incremental reallocations.

Note that this implementation is not synchronized, if multiple threads simultaneously access an ArrayList instance and at least one of them structurally modifies the list, then it must maintain external synchronization. Note that structural modification refers to any operation that adds or removes one or more elements, or explicitly resizes the underlying array. Merely setting the value of an element is not a structural modification.

 

Second realization

The default capacity of ArrayList is 10, and it is expanded by 1.5 times each time

To be continued. .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324497452&siteId=291194637