Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 236
Robin Rodricks Avatar asked Nov 13 '10 17:11

Robin Rodricks


1 Answers

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" ] );
like image 55
Grayson Lang Avatar answered Oct 03 '22 09:10

Grayson Lang