AS3:如何将向量转换为数组 [英] AS3: How to convert a Vector to an Array

查看:910
本文介绍了AS3:如何将向量转换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是一个矢量转换为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆