ArrayList Reuse?

RobEmDee

Centurion
Joined
Mar 25, 2003
Messages
130
Location
Richmond, VA
I have a situation in which I would like to dyamically populate an ArrayList and empty it frequently in an app I am developing. Does anyone have an opinion on the efficiency / resource effectiveness of doing this? Would it be better do Dim in a new ArrayList each time I do this (or Array because at the given point in time I would know the number of Items being populated) or simply empty and reuse the one I already have in memory? Thank you ahead of time for your response.
 
I'm sure one of the local gurus will slap me and tell me I'm wrong.. but what the heck I'll take a stab. :)

- If you know the size of your array and do not need to dynamically add or remove items, then just use an array instead of an ArrayList, as an ArrayList would have some slight overhead attached to it (accessing an object function etc has overhead).

- If you are starting from scratch each time then you might as well just do a new array entirely...
C#:
int[] myInts;
myInts = new int[5];
myInts[0] = 2;
// Ditch old array, create new one.
myInts = new int[5];
// etc..

However if you are dynamically adding/removing one item at a time, then obviously you wouldn't want to create a whole new array just for that.. use an ArrayList with Remove() and Add(). But like I said before, if you are just destroying everything in the array, then just create a fresh one. The reason I say this is that destroying the ArrayList is pointless and will cause slight overhead as pointing your array variable to a new array will mark your old one (the one you wanted to destroy) for garbage collection anyway, and it's always far more efficient to let the garbage collector handle junk you don't want anymore. :)

Okay.. now for a guru to come along and tell me how wrong I am. :D
 
Back
Top