I have an ArrayList object for which I know the exact size. Is there any way to specify that the ArrayList should not extend its capacity?
List<String> list = null;
int size = getSize(); // gets the exact number of elements I want
list = new ArrayList<String> (size);
for (int i = 0; i < size; i++) {
list.add("String num: " + i);
}
I don't want the ArrayList to re-size because that takes time which I want to avoid wasting.
list = new ArrayList<String> (size);
This will create arraylist with 'size' as initial capacity. As long as you don't add more elements than 'size' there will be no resizing.
Also please be sure that this really takes time in your application. Unless you have profiled and identified this as issue, you will not gain much by randomly optimizing the code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With