ArrayList:
ArrayList Use default parameterless constructor , Bottom Object The array length defaults to 10, When the length is not enough, use automatic growth 0.5 times
source code :
Default length 10
/** * Default initial capacity. */ private static final int DEFAULT_CAPACITY
= 10;
If the array length is not enough to grow 0.5 times grow method
int newCapacity = oldCapacity + (oldCapacity >> 1);
ArrayList Why is the query fast , Slow addition and deletion ?
Quick inquiry :
Because the array memory address arrangement is continuous , You can find it quickly, for example list.get(100); The first element memory address is 0x98, Then the memory address plus 100 I found it .
Slow addition and deletion :
increase :
Let's assume that the length of the array is 7, Now to add another element, what does it do ?
When adding elements , Check whether the array length is enough , If the bottom layer Object The array is not long enough , increase 0.5 Times the length , Form a new Object array , And set the previous array elements copy Into the new array
.
Delete :
Delete slow : When one of the data is deleted , All the data in the back should be moved to one position .
LinkedList:
Using linked list data structure , characteristic , Add and delete quickly , Slow query .
Slow query : The memory address before the element is discontinuous , You can only traverse one by one
increase :
When inserting data , Just change the direction of the memory address ;
Delete the memory address related to it , No one pointed to it , It becomes the object of garbage .
Technology
Daily Recommendation