Java – how many elements does ArrayList add when it resizes itself?
•
Java
Java's ArrayList dynamically extends itself when needed How many elements will it add when the extension occurs?
Does it copy the old array into the new array, or does it connect the two together in some way?
Solution
Look at the source code:
int newCapacity = (oldCapacity * 3)/2 + 1;
The exact factors vary from implementation to implementation, and GNU use factor 2 It doesn't matter. It just trades memory for speed
It copies all elements into a new 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
二维码