An overview of the
An ArrayList can be understood as a dynamic Array, or, according to MSDN, a complex version of Array. In contrast to an array, its size can grow dynamically. ArrayList is a mutable array implementation of the List interface. All optional list operations are implemented, allowing all elements including NULL.
Array features, query fast add and delete slow.
Each ArrayList instance has a capacity that points to the size of the array used to store the list elements. As you add more and more elements to an ArrayList, its capacity automatically grows, and this automatically increases the copying of data into a new array, so you can specify its capacity when constructing an ArrayList if you know how much data there is. An application can also use the ensureCapacity method to increase the capacity of an ArrayList instance before adding a large number of elements, reducing the amount of incremental reallocation.
Note: ArrayList is not thread-safe.
ArrayList principle
ArrayList implements the List interface. The bottom layer uses arrays to store all elements. Its operations are basically operations on arrays.
ArrayList inherits AbstractList and implements List. It is an array that provides related addition, deletion, modification, traversal, and so on.
ArrayList implements the RandmoAccess interface, which provides random access. RandmoAccess is a Java implementation used by lists to provide fast access to lists. In an ArrayList, you can get an element object quickly by its ordinal number, which is called fast random access.
ArrayList implements the Cloneable interface, which overrides the clone() function and can be cloned.
ArrayList implements the Java.io.Serizlizable interface, which means that ArrayList supports serialization and can be transmitted by serialization.
1.ArrayLIst is implemented using arrays
Other fields:
2. Constructor
3. Store
ArrayList provides a variety of ways to add elements.
(1) The add(E E) method adds the specified element to the end of the list. When capacity is insufficient, call grow to increase capacity.
As you can see, the core of this method is the Enrecapact Internal method. Used to adjust the size of an array, this function is at the heart of the automatic scaling mechanism. Its implementation is as follows:
That is, when you add more data, if the ArrayList becomes too small, you expand the array by 1.5 times.
(2)add(int, E) inserts the element at the specified position
(3) Set (int, E) replaces the element at the specified position and returns the value of the old element
AddAll (Collection) adds elements from the specified Collection to the end
(5)addAll(int, Collection) adds elements from the specified Collection to the specified location
The methods are pretty much the same, but the important thing is to expand the array
4. Read
It’s easy to read, and it’s easy to see why an ArrayList supports random access: its internal data structures are arrays, and arrays themselves support random access
5. Remove
(1)remove(int) Removes the element with the specified subscript and returns
(2)remove(Object) remove the specified element, return success (Boolean value)
Deleting an element in an array moves all elements following the element one place to the left.
6. Adjust the array capacity
The ensureCapacity method is used to adjust the size of an array