动作比较数组 [英] ActionScript Comparing Arrays

查看:185
本文介绍了动作比较数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何评估我的测试数组是否等于我的静态常量DEFAULT_ARRAY?应该不是我的输出将返回true?

 公共类MyClass的扩展Sprite
{
私有静态常量DEFAULT_ARRAY:阵列=新的Array(1,2,3);

公共职能MyClass的()
{
变种测试:数组=新阵列(1,2,3);
跟踪(测试== DEFAULT_ARRAY);
}

//痕迹假
 

解决方案

马科已经指出了这个问题。该 == 运营商将告诉你(仅供参考类型,比如数组对象),如果两个变量指向同一个对象。这显然​​不是这里的情况。你有2个不同的对象,即碰巧具有相同的内容。

那么,你很可能在寻找一种方法来比较两个数组是否具有相同的内容。这个看似简单的任务可能会更加由意义的。

标准的方法是使用一个功能是这样的:

 函数areEqual(A:阵列,B:阵列):布尔{
    如果(则为a.length!= b.length个){
        返回false;
    }
    VAR LEN:INT =则为a.length;
    对于(VAR我:= 0; I< LEN;我++){
        如果(A [1]!= = B〔I]){
            返回false;
        }
    }
    返回true;
}
 

这将工作在一些(可以说是大多数)案件。但它会失败,如果在任何一个阵列的项目有引用类型的语义(而不是值类型的语义)。基本上,数字(包括整数和uints),字符串,布尔,null和undefined有值类型的语义:

由于:

  VAR一:= 0;
变种B:INT = 0;
 

这将持有正确的:

 跟踪(A == B);
 

对于一切,与比较 == 只会返回true,如果两个瓦尔引用同一个对象:

  VAR一:对象= {数据:1};
变种B:对象= {数据:1};

微量(一== b)条; // 假
 

  VAR一:对象= {数据:1};
变种B:对象= A;

微量(一== b)条; // 真正
 

所以,如果您的数组包含对象(或者反过来其他阵列),该areEqual会在这种情况下失败:

  VAR arr_1:数组= [{数据:1}]。
VAR arr_2:数组= [{数据:1}]。

跟踪(areEqual(arr_1,arr_2));
 

为什么呢?因为 {数据:1} 您存储在 arr_1 对象是从不同的 {数据:1} 保存在 arr_2 对象

我不认为这是可以创建一个通用的功能,它检查两个数组'内容递归是否相等,因为这里的同时测定两个对象是否应被视为等于没有通用的方法。这真的取决于你的目标,你的域等即在您的应用程序,两个对象应该被视为相等如果他们有相同的 ID (假设你已经定义了一个 ID 属性)。因为我觉得这个节目,有没有办法知道手是什么让对象等于之前。

how can i evaluate whether my test array is equal to my static constant DEFAULT_ARRAY? shouldn't my output be returning true?

public class myClass extends Sprite
{
private static const DEFAULT_ARRAY:Array = new Array(1, 2, 3);

public function myClass()
{
var test:Array = new Array(1, 2, 3);
trace (test == DEFAULT_ARRAY);
}

//traces false

解决方案

Macke has already pointed out the problem. The == operator will tell you (for reference types such as Array objects) if two variables point to the same object. That's clearly not the case here. You have 2 different objects, that happen to have the same content.

So, what you're probably looking for is a way to compare whether 2 arrays have the same contents. This apparently simple task might be trickier than it seems.

The standard way is using a function like this:

function areEqual(a:Array,b:Array):Boolean {
    if(a.length != b.length) {
        return false;
    }
    var len:int = a.length;
    for(var i:int = 0; i < len; i++) {
        if(a[i] !== b[i]) {
            return false;
        }
    }
    return true;
}

This will work in some (arguably most) cases. But it will fail if the items in any of the arrays have reference type semantics (as opposed to value type semantics). Basically, Numbers (including ints and uints), Strings, Boolean, null and undefined have value type semantics:

Given:

var a:int = 0;
var b:int = 0;

This will hold true:

trace(a == b);

For everything else, comparing with == will only return true if both vars reference the same object:

var a:Object = {data:1};
var b:Object = {data:1};

trace(a == b); // false

But

var a:Object = {data:1};
var b:Object = a;

trace(a == b); // true

So, if your arrays contain objects (or in turn other Arrays), the areEqual will fail in this case:

var arr_1:Array = [{data:1}];
var arr_2:Array = [{data:1}];

trace(areEqual(arr_1,arr_2));

Why? Because the {data:1} object that you stored in arr_1 is different from the {data:1} object stored in arr_2.

I don't think it's possible to create a generic function that checks recursively whether two arrays' contents are equal, because there's no generic way of determinig whether two objects should be considered equal. This really depends on your objects, your domain, etc. I.e. in your app, two objects should be considered equal if they have the same ìd (assuming you have defined an ìd property). As I think this shows, there's no way to know before hand what makes to objects equal.

这篇关于动作比较数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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