当前位置:网站首页>ArrayList automatic capacity expansion principle
ArrayList automatic capacity expansion principle
2022-06-12 10:36:00 【@2206】
Preface
ArrayList The underlying data structure is Array ,
- Array is characterized by : Quick query , Add or delete slowly
The reason is : Sequential storage , There is an index , According to the index , Navigate directly to the element , So the inquiry is fast ; Because it's sequential storage , Add or delete , Will have an impact on subsequent elements .
1. First, create a ArrayList Assembly begins , Set it up as follows :
ArrayList<String> list = new ArrayList();
2. After entering the source code, you can see the following source code :
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
transient Object[] elementData; // non-private to simplify nested class access
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {
};
- First ArrayList There is an assignment code in the construction method of , To the left of the equals sign is a customized source code Object Array of constants of type , Then assigned to transient A retouched one Object Type of elementData Array .
- The above conclusion is when creating objects ArrayList The bottom layer creates an array , The initial capacity of the array is empty .
ArrayList For the first time in the set add Elements :
1. First call add What do you see as methods , You also need to enter the source code .
list.add("aa");
2.add The source code of the method is as follows :
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
call add Method to pass in a string “aa”.
add The first sentence in the method body calls ensureCapacityInternal Method , Chinese means :( Determine internal capacity ) The minimum capacity passed to the inside is 1, because size It is a customized source code int The variable of type has no initial value, so size by 0.
The second sentence is in the empty array size++, here elementData The array is 0, Assign the passed value to the array 0 Subscript .
Get into ( Determine internal capacity ) Method , The minimum capacity at this time is 1.
private void ensureCapacityInternal(int minCapacity) {
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}
5. Another... Is called in the method of determining the internal capacity ( Computing capacity ) Two parameters are passed in the method : One is an empty array , One is the minimum capacity . And then catch up with the source code .
private static int calculateCapacity(Object[] elementData, int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
return minCapacity;
}
/** * Default initial capacity. */
private static final int DEFAULT_CAPACITY = 10;
6. At this point, a judgment is made in this method , Condition is true, because This statement is the one called at the bottom when creating the collection object .
7. call Math Class to compare the maximum value , The minimum capacity is 1,DEFAULT_CAPACITY The constant is 10, Because the source code defines and assigns the initial value to 10.
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
8. Do judgment 10 reduce 0 Greater than 0 Conditions established , call grow Method will have the minimum capacity 10 Put it in .
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
9. here minCapacity by 10. The second and last sentences of the source code are the core .
newCapacity = minCapacity; After the old capacity is assigned to the new capacity , The new capacity is now 10.
The last sentence is to add new capacity 10 adopt Arrays In a tool class copyof Method is copied to elementData, And assigned to elementData, To the left of the equal sign elementData Point to the original empty array . At this point, the new array length becomes 10 了 .
Conclusion : First call add When the method is used , The array length is 10.
ArrayList The second call of the collection add Method add element :
list.add("bb");
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
- The array length is 10,aa Has been deposited in 0 Subscript ,size++ Now become 1 了 . The minimum capacity is now 2.
private void ensureCapacityInternal(int minCapacity) {
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}
private static int calculateCapacity(Object[] elementData, int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
return minCapacity;
}
// The meaning of the secondary code is whether to confirm the capacity expansion ,if If the condition is satisfied, the capacity expansion shall be implemented .
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
- here elementData by 10,minCapacity by 2.
- If the condition does not hold, it will not call grow Method . When the 11 It will only be called when the data is retrieved grow Capacity expansion method .
minCapacity by 11 Call the capacity expansion method : Let's expand it to the original 1.5 times .
The expansion code is selected from the box above .
The final summary :
ArrayList The underlying data structure is an array , When creating a ArrayList Object time , The bottom layer initializes an empty array , An array is Object type , The array name is elementData. When you first add an element , The array length is expanded to 10.
When the first 11 Add times , Will trigger the expansion mechanism , In fact, it's called grow Method , Expand to the length of the original array 1.5 times .
At each expansion , Is to create a new array , Pass the elements of the old array through Arrays The tool class is copied to the new array .elementData Points to the new array
*** The expansion is not based on the splicing of old arrays , And create a 1.5 A new array of times the length , And copy the elements of the old array to the new array .
边栏推荐
- The solution of Lenovo notebook ThinkPad t440 WiFi dropping all the time
- Unable to load dynamic library ‘oci8_ 12C 'or unable to load dynamic library' PDO_ OCI 'or cannot find module
- VSCode代码调试技巧
- PHP occupies memory
- Solution to the problem that the applet developer tool cannot input simplified Chinese
- Mobile terminal commissioning
- How to refund the pre-sale deposit of JD 618 in 2022? Can JD 618 deposit be refunded?
- Propagation of transactions
- How PLC constructs shift function block (FC) by itself
- 浅谈调和形状上下文特征HSC对3DSC的改进
猜你喜欢

properties中文乱码

conda 安装tensorflow 测试tensorflow

Is the acceptance standard a test case?

2022京东618预售定金怎么退?京东618定金能退吗?

使用cpolar远程办公(2)

Fiddler automatically saves the result of the specified request to a file

2022淘宝618超级喵运会玩法来了 超级喵运会有哪些攻略方法

MYSQL用户与权限管理,角色管理

2022京東618預售定金怎麼退?京東618定金能退嗎?

Circuitbreaker fuse of resilience4j - Summary
随机推荐
Assign a specified amount to a specified number of people at random
On 3dsc theory and application of 3D shape context feature
JS open common application market
PHP wechat payment V3 interface
Malicious code analysis practice - lab03-01 Exe basic dynamic analysis
Solution to invalid small program scroll into view
93. 获得内网的所有IP地址
Machine learning is not something you can use if you want to use it
ASP.NET Core权限系统实战(零)
The solution of Lenovo notebook ThinkPad t440 WiFi dropping all the time
Getting started with cloud API basics -- basic knowledge of picgo writing plug-ins
How Qualcomm platform modifies special voltage
Binassii module - converting between binary and ASCII
Error during session start; please check your PHP and/or webserver log file and configure your PHP
使用cpolar远程办公(2)
Chapter 3 search
Pseudo static setting of access database in win2008 R2 iis7.5
PHP Apple internal purchase callback processing
2022 JD 618 Comment rembourser le dépôt de pré - vente? Le dépôt JD 618 peut - il être remboursé?
file_ get_ Contents() JSON after reading_ Decode cannot be converted to array