Analysis of ArrayList source code of Java collection

summary

ArrayList can be understood as a dynamic array. According to MSDN, it is a complex version of array Compared with array, its capacity can grow dynamically ArrayList is the implementation of variable array of list interface All optional list operations are implemented, and all elements including null are allowed

Array features fast query and slow addition and deletion

Each ArrayList instance has a capacity that points to the size of the array used to store list elements With the continuous addition of elements to the ArrayList, its capacity will increase automatically. Automatic growth will lead to the re copy of data to the new array. Therefore, if the amount of data can be predicted, its capacity can be specified when constructing the ArrayList Before adding a large number of elements, the application can also use the ensurecapacity method to increase the capacity of ArrayList instances and reduce the number of incremental redistribution

Note: ArrayList is not thread safe

ArrayList principle

ArrayList implements the list interface, and the underlying uses array to save all elements. Its operation is basically array operation

ArrayList inherits abstractlist and implements list It is an array that provides related add, delete, modify, traverse, etc

ArrayList implements randmoaccess interface, which provides random access function Randmoaccess is implemented by list in Java to provide fast access for list In ArrayList, you can quickly obtain element objects by element sequence number, which is fast random access

ArrayList implements the clonable interface, that is, it overrides the function clone (), which can be cloned

ArrayList implements Java io. Serializable interface, which means that ArrayList supports serialization and can be transmitted through serialization

1. ArrayList bottom layer is implemented by array

Other fields:

2. Constructor

3. Storage

ArrayList provides a variety of ways to add elements

(1) Add (E) method to add the specified element to the end of the list When the capacity is insufficient, call grow to increase the capacity

As you can see, the core content of the method is the ensurecapacityinternal method Used to adjust the array capacity. This function is the core of the automatic capacity expansion mechanism The implementation is as follows:

In other words, when adding data, if the ArrayList size is not enough, expand the array to 1.5 times the original size

(2) Add (int, e) inserts an element at the specified position

(3) Set (int, e) replaces the element at the specified position and returns the value of the old element

(4) Addall (Collection) adds the elements in the specified collection to the end

(5) Addall (int, collection) adds the elements in the specified collection to the specified location

The methods are basically the same. The important thing is to expand the capacity of the array

4. Read

The method of reading is relatively simple. The reason why ArrayList can support random access is obvious, because its internal data structure is array, and the array itself supports random access

5. Delete

(1) Remove (int) deletes the element with the specified subscript and returns

(2) Remove (object) deletes the specified element and returns whether it succeeds (Boolean value)

Deleting an element in an array will move all elements following the element one position to the left

6. Adjust array capacity

The ensurecapacity method is used to adjust the capacity of the array

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>