6

I am using Vectors in Flash 10 for the first time, and I want to create it in the same way I used to do with Arrays, e.g:

var urlList : Array = [url1, url2, url3];

I have tried various different methods but none seem to work, and I have settled on the following as a solution:

var urlList : Vector.<String> = new Vector.<String>();
urlList.push(url1, url2, url3);

Is this even possible?

animuson
  • 53,861
  • 28
  • 137
  • 147
soulBit
  • 438
  • 6
  • 15

2 Answers2

22

When it doubt, check the AS3 docs. :)

var urlList : Vector.<String> = new <String>["str1", "str2", "str3"];
trace(urlList);

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Vector.html#Vector()

Direct quote of the line I adapted this from in the documentation:

To create a pre-populated Vector instance, use the following syntax instead of using the parameters specified below:

 // var v:Vector.<T> = new <T>[E0, ..., En-1 ,];
 // For example: 
 var v:Vector.<int> = new <int>[0,1,2,];
6

You coerce an array to a Vector:

var urlList:Vector.<String> = Vector.<String>([url1, url2, url3]);
Sean Fujiwara
  • 4,506
  • 22
  • 34
  • If I do this my build fails :( – soulBit May 27 '11 at 20:58
  • @soulBit With what compiler? This has been supported since vectors were introduced as far as I know. – Sean Fujiwara May 27 '11 at 21:05
  • Actually soulBit it turns out this does work, so it does at least deserve an upvote. :) –  May 27 '11 at 21:11
  • 1
    Actually I wouldn't use this method, because it's proven to be slower than the version provided by @Digital Architect 100 000 pre populated vectors with this method took over 800ms, the other version a little over 500 ms – Creative Magic Dec 18 '13 at 03:23
  • Makes sense that it's slower. Why would you perform a type coercion from Array to Vector just to define a vector? The correct answer is the other one, as defined in Adobe's documentation: new [...]; – OMA Nov 15 '16 at 14:46