转换 Vector.到数组?
不幸的是,在 Actionscript 中,似乎尚未完全支持 Vector 类。在某些情况下,我需要将 Vector 转换为数组(例如创建 ArrayCollection)。我认为这可以解决问题:
var myVector:Vector.<MyType> = new Vector.<MyType>();
var newArray:Array = new Array(myVector);
显然这只是创建一个数组,其中数组的第一个索引包含完整的 Vector 对象。这是我唯一的选择:
var newArray:Array = new Array(myVector);
for each(var item:MyType in myVector)
{
newArray.push(item);
}
我觉得这会让代码变得很混乱,我需要在很多地方这样做。 Vector 类没有实现任何类型的接口,因此据我所知,我无法创建通用函数来转换为数组。有没有什么方法可以做到这一点,而不需要每次我想将 Vector 转换为数组时都添加这些混乱?
Unfortunately in Actionscript, it seems like support for the Vector class isn't fully there yet. There are some scenarios where I need to convert a Vector into an array (creating an ArrayCollection for example). I thought this would do the trick:
var myVector:Vector.<MyType> = new Vector.<MyType>();
var newArray:Array = new Array(myVector);
Apparently this just creates an array where the first index of the array contains the full Vector object. Is this my only option:
var newArray:Array = new Array(myVector);
for each(var item:MyType in myVector)
{
newArray.push(item);
}
I feel like that clutters up the code a lot and I need to do this in a lot of places. The Vector class doesn't implement any kind of interface, so as far as I can tell I can't create a generic function to convert to an array. Is there any way to do this without adding this mess every time I want to convert a Vector to an array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有简单/快速的方法来做到这一点,最好的解决方案是使用像这样的实用程序类:
然后您只需将代码更新为如下所示:
There's no easy/fast way to do it, the best solution is to use an utility class like this one:
Then you just have to update your code to something like this:
保罗在工作发现
Paul at Work found a better way to do it.