In my Data Structures class we have studies the Java ArrayList class, and how it grows the underlying array when a user adds more elements. That is understood. However, I ca
An ArrayList doesn't automatically shrink back, as far as i know. However, you can say something like:
ArrayList al = new ArrayList();
// fill the list for demo's sake
for (int i = 0; i < 1000000; ++i)
{
al.add(i);
}
// now remove all but one element
al.removeRange(1, al.size());
// this should shrink the array back to a decent size
al.trimToSize();
Note, the amount of memory available probably won't change til the GC runs again.