posted 19 years ago
[Yuma]: thereafter a copy operation is done to load additional elements in sets of 10
No, ArrayList does not keep incrementing in sets of ten. Whenever it detects the need to resize its internal array, it determines the new size using the formula newCapacity = (oldCapacity * 3)/2 + 1 (you can see this in the source for JDK 1.42 - presumably other versions are similar). This results in fewer copies overall.
does the "size" parameter of ensureCapacity replace the default of 10 and causes the array to resize past the "size" specified?
No, future resizing will still follow the formula above.
Hence, does that improve performance?
You can use ensureCapacity() to improve performance, yes. If I know that a list will have at least 1000 names in it, I might presize the list to 2000 or so. That way it doesn't have to start at 10, then resize to 16, then resize to 25, then 38, 58, 88, 133, 200, 301, 452, 679, 1019, 1529...
"I'm not back." - Bill Harding, Twister