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

发布于 2024-10-02 11:05:55 字数 547 浏览 8 评论 0 原文

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

我尝试了这个,它符合要求,但不起作用!

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

我还需要将任意数组加载到向量中,如下所示:

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

有办法做到这一点吗?


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

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

我尝试了这个,但它给了我错误:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鸢与 2024-10-09 11:05:55

初始化字符串向量的适当语法是这样的:

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

为了创建多维向量,请使用以下语法:

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

请注意,以下语法可以工作,但不太理想,因为它首先生成一个数组,然后将其转换为向量,这样速度较慢,对于非常大的数组有问题,并且不支持多维向量。

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

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" ] );
ぽ尐不点ル 2024-10-09 11:05:55

您可以使用 Vector.。 html#Vector%28%29" rel="nofollow">Vector. 全局函数

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

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

var vec : Vector.<String> = Vector.<String>(["en","fr"]);
没︽人懂的悲伤 2024-10-09 11:05:55

我认为您不能将数组数组传递到 Vector 中:

Vector.<Vector.<Number>>

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

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

当然,这将导致所有内容的新副本被创建两次,因此理想情况下您不会转换大列表。

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.

怪我太投入 2024-10-09 11:05:55

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

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

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

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

最重要的是,它将在编译时执行类型检查,并减少出现运行时错误的机会。

对于 2D 向量,没有直接语法,因此您必须显式创建每个向量Vector:

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

您可以在这里深入了解非空 Vector 初始化的性能以及为什么其他解决方案速度较慢:

http: //jacksondunstan.com/articles/702

PS:如果右括号没有用空格分隔,较旧的 mxmlc 编译器将无法理解右括号:

new >

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文