如何使用值数组初始化向量?

时间:2021-04-13 13:31:42

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?

编辑:如何使用2D数组值初始化2D矢量?

 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

TypeError:错误#1034:类型强制失败

4 个解决方案

#1


3  

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

我不认为你可以将一个数组数组传入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:

类型强制不适用于复杂类型。如果您已经拥有2D数组,请考虑以下转换代码:

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


49  

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.

请注意,以下语法有效,但不太理想,因为它首先生成一个数组,然后将其强制转换为Vector,后者速度较慢,存在非常大的数组问题,并且不支持多维向量。

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

#3


4  

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

您可以使用Vector。 全局函数从数组初始化Vector。

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

#4


2  

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

从值列表初始化Vector的最干净,最快速和最安全的方法是:

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 :

它不会创建一个临时数组来初始化新的Vector,因此它将生成最快的字节码,并且不会因无用的临时阵列实例而烦扰垃圾收集器。它和以下一样快,但更实用:

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:

对于2D矢量,没有直接语法,因此您必须显式创建每个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 :

您可以获得有关非空Vector初始化性能的深入信息,以及为什么其他解决方案在这里变慢:

http://jacksondunstan.com/articles/702

http://jacksondunstan.com/articles/702

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

PS:较旧的mxmlc编译器如果没有与空格分隔,则不会理解右括号:

new <Vector.<Number>>

new >

#1


3  

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

我不认为你可以将一个数组数组传入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:

类型强制不适用于复杂类型。如果您已经拥有2D数组,请考虑以下转换代码:

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


49  

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.

请注意,以下语法有效,但不太理想,因为它首先生成一个数组,然后将其强制转换为Vector,后者速度较慢,存在非常大的数组问题,并且不支持多维向量。

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

#3


4  

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

您可以使用Vector。 全局函数从数组初始化Vector。

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

#4


2  

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

从值列表初始化Vector的最干净,最快速和最安全的方法是:

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 :

它不会创建一个临时数组来初始化新的Vector,因此它将生成最快的字节码,并且不会因无用的临时阵列实例而烦扰垃圾收集器。它和以下一样快,但更实用:

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:

对于2D矢量,没有直接语法,因此您必须显式创建每个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 :

您可以获得有关非空Vector初始化性能的深入信息,以及为什么其他解决方案在这里变慢:

http://jacksondunstan.com/articles/702

http://jacksondunstan.com/articles/702

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

PS:较旧的mxmlc编译器如果没有与空格分隔,则不会理解右括号:

new <Vector.<Number>>

new >