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?

2 Answers 2

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,];
3
  • This works correctly, although I'm using FDT and it breaks a bunch of formatting :S Strange - maybe it's time for a new question!
    – soulBit
    May 27, 2011 at 21:01
  • Interesting syntax. +1 since you get compile time type checking. May 27, 2011 at 21:04
  • This didn't work for me, and then I realized it's because I'm using Flash CS4. I was wondering why I wasn't familiar with this syntax. :P Apr 5, 2017 at 15:58
6

You coerce an array to a Vector:

var urlList:Vector.<String> = Vector.<String>([url1, url2, url3]);
5
  • If I do this my build fails :(
    – soulBit
    May 27, 2011 at 20:58
  • @soulBit With what compiler? This has been supported since vectors were introduced as far as I know. May 27, 2011 at 21:05
  • Actually soulBit it turns out this does work, so it does at least deserve an upvote. :)
    – user562566
    May 27, 2011 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 Dec 18, 2013 at 3: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 <String>[...];
    – OMA
    Nov 15, 2016 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.