As the title mentions, if I create an ArrayList with initial capacity 500 and then clear it after some time will its capacity still be 500 ? or do I need to re-init it for that ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, it preserves its capacity. (at least, not in the implementation of the Oracle VM):
Just to be clear: an
ArrayListis backed by an array (e.g. int[] for ArrayList) and that array is expanded whenever you go over capacity by creating a new array and copying things over. Clearing will not (as seen in the code) create a new smaller array, copy things there, and destroy the old big array.