Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is a function called forEach which both Vector and Array has which you can use. Basically it calls a function for each element in the vector. This is how it works:</p> <pre><code>var myVector:Vector.&lt;Foo&gt; = new Vector(); var myArray:Array = []; myVector.forEach(arrayConverter); function arrayConverter(element:*, index:int, array:Array):void{ myArray[myArray.length] = element; } </code></pre> <p>But I couldn't find a function which just moves all the values from the Vector to an Array. Another solution could be that you create a class which extends the Vector class and then you have a public function called toArray() and then you have that code in that function so you don't have to write it each time you want to convert. </p> <p><a href="http://help.adobe.com/en_US/AS3LCR/Flash_10.0/Vector.html" rel="noreferrer">Vector documentation</a> </p> <p><strong>Edit:</strong> Found this old question today and thought it would be interesting to do a benchmark test of all the different methods this sunday morning. </p> <p>I used a vector with 1000000 items in and made 2 tests for each loop. One using the built in array functions push and one using regular array operations.</p> <ul> <li>For loop, not push: <strong>520</strong> ms</li> <li>For loop, push: <strong>1027</strong> ms</li> <li>Foreach loop, not push: <strong>1753</strong> ms</li> <li>Foreach loop, push: <strong>2264</strong> ms</li> <li>While loop, not push: <strong>2775</strong> ms</li> <li>While loop, not push: <strong>3282</strong> ms</li> <li>Util loop, not push: <strong>4059</strong> ms</li> <li>Util loop, push: <strong>4570</strong> ms</li> </ul> <p>And here is a benchmark using 1000 items:</p> <ul> <li>For loop, not push: <strong>1</strong> ms</li> <li>For loop, push: <strong>2</strong> ms</li> <li>Foreach loop, not push: <strong>2</strong> ms</li> <li>Foreach loop, push: <strong>3</strong> ms</li> <li>While loop, not push: <strong>3</strong> ms</li> <li>While loop, not push: <strong>4</strong> ms</li> <li>Util loop, not push: <strong>4</strong> ms</li> <li>Util loop, push: <strong>5</strong> ms</li> </ul> <p>Basically it's when you get over 10 000 items you start to see the real difference. So between 0 and 10 000 items it doesn't really matter which you use. </p> <pre><code>package { public class Loops{ public static function forLoop(vector:Vector.&lt;Foo&gt;, usePush:Boolean = false):Array{ var result:Array = []; for(var i:int = 0; i &lt; vector.length; i++){ if(usePush){ result.push(vector[i]); }else{ result[result.length] = vector[i]; } } return result; } public static function foreachLoop(vector:Vector.&lt;Foo&gt;, usePush:Boolean):Array{ var result:Array = []; for each(var key:String in vector){ if(usePush){ result.push(vector[key]); }else{ result[result.length] = vector[key]; } } return result; } public static function whileLoop(vector:Vector.&lt;Foo&gt;, usePush:Boolean):Array{ var result:Array = []; var i:int = 0; while(i &lt; vector.length){ if(usePush){ result.push(vector[i]); }else{ result[result.length] = vector[i]; } } return result; } public static function arrayUtilLoop(vector:Vector.&lt;Foo&gt;, usePush:Boolean):Array{ var result:Array = []; function arrayUtilForeach(element:*, index:int, array:Array):void{ if(usePush){ array.push(element); }else{ array[result.length] = element; } } vector.forEach(arrayUtilForeach); return result; } } } </code></pre> <p>I used the getTimer function to do the benchmark, so it might not be 100% accurate but there's so much difference between the different loops so I don't think it matters.</p> <p>I hope someone found this useful. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload