转换 Vector.到数组?

发布于 2024-10-21 16:00:57 字数 558 浏览 6 评论 0原文

不幸的是,在 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 技术交流群。

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

发布评论

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

评论(2

心欲静而疯不止 2024-10-28 16:00:57

没有简单/快速的方法来做到这一点,最好的解决方案是使用像这样的实用程序类:

package {

    public class VectorUtil {

        public static function toArray(obj:Object):Array {
            if (!obj) {
                return [];
            } else if (obj is Array) {
                return obj as Array;
            } else if (obj is Vector.<*>) {
                var array:Array = new Array(obj.length);
                for (var i:int = 0; i < obj.length; i++) {
                    array[i] = obj[i];
                }
                return array;
            } else {
                return [obj];
            }
        } 
    } 
}

然后您只需将代码更新为如下所示:

var myArray:Array = VectorUtil.toArray(myVector);

There's no easy/fast way to do it, the best solution is to use an utility class like this one:

package {

    public class VectorUtil {

        public static function toArray(obj:Object):Array {
            if (!obj) {
                return [];
            } else if (obj is Array) {
                return obj as Array;
            } else if (obj is Vector.<*>) {
                var array:Array = new Array(obj.length);
                for (var i:int = 0; i < obj.length; i++) {
                    array[i] = obj[i];
                }
                return array;
            } else {
                return [obj];
            }
        } 
    } 
}

Then you just have to update your code to something like this:

var myArray:Array = VectorUtil.toArray(myVector);
日久见人心 2024-10-28 16:00:57

保罗在工作发现

var newArray:Array = [].concat(myVector);

Paul at Work found a better way to do it.

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