Andreas said:
I'd rather think that the internal array that keeps
the refs is dumped on .clear(), without any walk-through.
I looked at the source code (JRE 1.5), and it does walk through the
array. It might be more efficient to do it the way you say.
That does create a significant difference between clear() and creating a
new ArrayList. The new ArrayList will start with a small array, and
build it up as needed for the elements added after the clearing. Calling
clear leaves the array at the size it was immediately before.
Using clear() might be the better choice if a lot of CPU time is going
on copying in ArrayList as the list rebuilds after the clear. On the
other hand, new might be the better choice if the ArrayList was very
long, will have fewer elements in the future, and the program is using a
lot of memory.
However, there is an important functional difference. If any copies of
the "list" variable escaped, the other holders of references will go on
seeing the old ArrayList if new is used, but will see their list drop to
zero elements after clear. I would code this however best expresses the
intent, and be very careful about changing it for performance tuniing.
Patricia