AS3:如何将向量转换为数组 [英] AS3: How to convert a Vector to an Array
问题描述
在 Actionscript3 中将向量转换为数组的最佳方法是什么?
What's the nicest way to convert a Vector to an Array in Actionscript3?
正常的转换语法不起作用:
The normal casting syntax doesn't work:
var myVector:Vector.<Foo> = new Vector();
var myArray:Array = Array(myVector); // calls the top-level function Array()
由于Array函数的存在.上面的结果是一个数组,但它是一个由原始向量组成的单个元素的数组.
due to the existance of the Array function. The above results in an array, but it's an array with a single element consisting of the original Vector.
这使得稍微冗长:
var myArray:Array = new Array();
for each (var elem:Foo in myVector) {
myArray.push(elem);
}
这很好,我想,虽然有点罗嗦.这是执行此操作的规范方法,还是标准库中隐藏了一个 toArray() 函数?
which is fine, I guess, though a bit wordy. Is this the canonical way to do it, or is there a toArray() function hiding somewhere in the standard library?
推荐答案
你的方法是最快的......如果你认为它很冗长,那么构建一个实用函数...... :)
your approach is the fastest ... if you think it's to verbose, then build a utility function ... :)
要构建实用程序函数,您可能必须按如下方式删除类型:
To build a utility function, you will probably have to drop the type as follows:
function toArray(iterable:*):Array {
var ret:Array = [];
for each (var elem:Foo in iterable) ret.push(elem);
return ret;
}
这篇关于AS3:如何将向量转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!