Why Java ArrayList's remove() method implementation does not decrease the size of internal array data? -


this question has answer here:

for add() method, implementation such elementdata\[\] array's size grows needed. however, looking @ remove() method, not shrink size elements removed.

i tested using simple code , elementdata[] starts out 10 , grows. however, when delete elements using remove() method, size of elementdata[] stays @ point finished adding elements.

int testsize = 10000000;  arraylist<integer> alist = new arraylist<integer>(); // size of elementdata[] 10 for(int = 0; < testsize; i++) {     alist.add(i); } // size of elementdata[] 13845150 for(int = alist.size()-1; >= 0; i--) {     alist.remove(i); } // size of elementdata[] remains @ 13845150 

isn't wasting memory?

first should note size of array cannot changed, once created. changing size of underlying array of arraylist requires entire content of array copied new array.

when increasing size, must allocate larger array, otherwise not fit in new elements. however, it's not worth performance overhead copy when remove list. memory lower concern performance.


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -