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 .


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 .

©2019-2020 Toolsou All rights reserved,
1190 Reverses the substring between each pair of parentheses leetcodemysql Joint index details You don't know ——HarmonyOS Create data mysql Library process Character recognition technology of vehicle license plate based on Neural Network A guess number of small games , use JavaScript realization Talking about uni-app Page value transfer problem pytorch of ResNet18( Yes cifar10 The accuracy of data classification is achieved 94%)C++ Method of detecting memory leak One is called “ Asking for the train ” A small village Finally got the train