How do I initialize a vector with an array of valu

2020-07-11 04:52发布

问题:

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

回答1:

I don't think that you can pass in an array of arrays into the Vector:

Vector.<Vector.<Number>>

The type coercion doesn't work for a complex type. If you already have the 2D Array consider the following conversion code:

var numbers:Array = [[1, 2, 3], [4, 5, 6]];
var numbersTemp:Array =
numbers.map(
    function (element:*, index:int, arr:Array):Vector.<Number> {
    return Vector.<Number>(element);
});
var nums:Vector.<Vector.<Number>> = Vector.<Vector.<Number>>(numbersTemp);

Of course this will cause new copies of everything to be created twice, so ideally you are not converting big lists.



回答2:

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" ] );


回答3:

You can initialise a Vector.<T> from an array by using the Vector.<T> global function:

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


回答4:

The cleanest, fastest and most type-safe way to initialize a Vector from a list of values is :

langs = new <String> ["en","fr"];

it will not create a temporary Array to initialize the new Vector, so it will generate the fastest bytecode, and will not bother the garbage collector with useless temporary Array instantiations. It's as fast as, but more practical than :

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

Most importantly, it will perform type checking at compile time, and reduce the chance of getting run time errors

For 2D Vectors, there is no direct syntax, so you'll have to explicitly create each Vector:

nums = new <Vector.<Number> > [new <Number>[10,20,30], new <Number>[10,20,30]];

You can have in-depth information on performance of non-empty Vector initialization and why other solutions are slower here :

http://jacksondunstan.com/articles/702

PS:older mxmlc compilers will not understand the closing brackets if they are not separated bv a space:

new <Vector.<Number>>