How do I initialize a vector with an array of values?

Robin Rodricks picture Robin Rodricks · Nov 13, 2010 · Viewed 15k times · Source

How do I initialize a vector with an array of values?

I tried this and it complies fine, but does not work!

 langs = new Vector.<String>(["en","fr"]);

I also need to load an arbitrary array into a vector, like this:

 langlist = ["en","fr"];
 langs = new Vector.<String>(langlist);

Is there a way to do this?


Edit: How do I initialize a 2D vector with a 2D array of values?

 numbers = [[10,20,30], [10,20,30]];
 nums = Vector.<Vector.<Number>>(numbers);

I tried this but it gives me the error:

TypeError: Error #1034: Type Coercion failed

Answer

Grayson Lang picture Grayson Lang · Jun 7, 2011

The appropriate syntax for initializing a Vector of Strings is this:

var langs:Vector.<String> = new <String>[ "en","fr" ];

In order to create multidimensional Vectors use the following syntax:

var v:Vector.<Vector.<int>> = new <Vector.<int>>[ new <int>[ 1, 2, 3 ], new <int>[ 4, 5, 6 ] ];

Note that the following syntax works but is less desirable because it first generates an Array and then casts it to a Vector, which is slower, has issues with very large Arrays, and doesn't support multidimensional Vectors.

var langs:Vector.<String> = Vector.<String>( [ "en","fr" ] );