Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to empty a Vector or Array in AS3?

What is the most efficient way to empty an Array or Vector in ActionScript 3?

I've always just re-initialized them:

vector = new Vector.<T>();
array = [];

It doesn't look like there's an empty() function or anything similar.

Is there a better way?

like image 934
Marty Avatar asked Feb 17 '12 02:02

Marty


2 Answers

Re-initializing the array is fine in most cases, since the garbage collector will just sweep up the old array. Still, if you want to empty an array without creating a new one you can set array.length = 0

like image 96
jhocking Avatar answered Nov 14 '22 21:11

jhocking


Another option is to use the splice method.

Array::splice documentation

For an array the following call empties it:

array.splice(0);

For a vector the second parameter is enforced, so the call becomes:

vector.splice(0, vector.length);
like image 6
Maic López Sáenz Avatar answered Nov 14 '22 21:11

Maic López Sáenz