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

徘徊边缘 提交于 2019-12-06 00:46:16

问题


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>>



来源:https://stackoverflow.com/questions/4173845/how-do-i-initialize-a-vector-with-an-array-of-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!