在AS3中,你在哪里画字典和ArrayCollection中之间的界限?界限、字典、你在哪里、ArrayCollection

2023-09-08 15:08:43 作者:海水不比泪咸

基本上,我一直在用我的程序Dictionary对象,基本上把整数作为它的键和存储RTMFP对标识在相应的位置。每个INT是独一无二的,并重新presented一个用户。

Basically I have been using a Dictionary object in my program that basically took ints as its keys and stored RTMFP peer IDs in the appropriate locations. Each int was unique and represented one user.

现在我需要扩大对这个,用户通过int和一个布尔值的组合确定的,有点像这样的:

Now I'm needing to expand on this where users are identified by a combination of the int and a Boolean value, kind of like this:

private var m_iUID:int;
private var m_blnIsCurrent:Boolean;

只有这两个之间的结合真是唯一标识用户。话虽这么说,我只是用做出来的这种对词典按键的新类;但随后发生,我认为不是做这种方式,我可能只是同行ID添加到类定义,并打开Dictionary对象为ArrayCollection:

Only the combination between those two really uniquely identifies the user. That being said I was just about to use a new class made out of this for the Dictionary keys; but then it occurred to me that instead of doing it this way, I could just add the peer ID to the class definition and turn the Dictionary object into an ArrayCollection:

private var m_iUID:int;
private var m_blnIsCurrent:Boolean;
public var m_strNearID:String;

所以,现在我不知道这是在这种情况下真是再好不过了。而这个问题已经导致了一个更大的问题:你在哪里真正划清这两种集合类型之间的界限带来什么影响?他们突然开始似乎并不毕竟所有的不同的,除非你想避免与类定义搞乱。我想我真的问讨教一下双方的具体方案和一般性的问题。谢谢!

So now I'm wondering which is really better in this scenario. And that question has led to a bigger question: where do you really draw the line between these two collection types in general? They're suddenly starting to not seem all that different after all, except where you're trying to avoid messing with class definitions. I guess I'm really asking for advice about both the specific scenario and the general question. Thanks!

推荐答案

ArrayCollection的仅仅是一个包装的阵列,并且只适用于Flex的。

ArrayCollection is just a wrapper for an Array, and is only available in Flex.

在AS3中,你真的有3个基本的哈希表类型:数组,对象和字典。你选择哪一个使用基于您要使用的密钥的类型:一个整数,字符串或对象引用。阵列将转换任何键为int,对象将任意键转换为字符串。字典的作品就像对象的字符串键(将转换的元的为一个字符串),但它是真正的好是使用对象的引用的作为键。

In AS3 you really have 3 fundamental hash table types: Array, Object, and Dictionary. You choose which one to use based on the type of key you want to use: an integer, a string, or an object reference. Arrays will convert any key to an int, Object will convert any key to a string. Dictionary works like Object for string keys (and will convert primitives to a string) but what it is really good at is using object references as keys.

这要使用单个int作为唯一的密钥,使用数组。如果你想用一个字符串作为唯一键,使用对象。如果你想使用对象引用作为唯一键,使用字典。

It you want to use a single int as the unique key, use an array. If you want to use a single string as the unique key, use an object. If you want to use object references as the unique key, use a Dictionary.

在你的情况,你应该使用一个对象,并在你的钥匙类的定制的toString()方法。这是因为要使用原始值的复合(不是对象引用)作为唯一键。有没有办法做到这一点本身,所以你必须要混搭的值加在一起作为一个字符串。对象是最好的(最快)的哈希字符串键的表,所以这是你应该使用的集合。

In your case you should probably use an Object, and a custom toString() method on your "key" class. This is because you want to use a composite of primitive values (NOT an object reference) as your unique key. There is no way to do this natively, so you'll have to mash the values together as a single string. Objects are the best (fastest) hash table for string keys, so that is the collection you should use.

例如:

class User {
    private var m_iUID:int;
    private var m_blnIsCurrent:Boolean;
    public var m_strNearID:String;
    public function User(UID:int, IsCurrent:Boolean) {
        m_iUID = UID;
        m_blnIsCurrent = IsCurrent;
    }

    // Custom toString to mash together primitives
    public function toString() {
        return m_iUID.toString() + "-" + (m_blnIsCurrent ? "1" : "0");
    }
}

// Later:
var allUsers:Object = {}
var user1:User = new User(231049, true);
var user2:User = new User(0x2309, false);

// Implicitly calls toString():
allUsers[user1] = "User 1";
allUsers[user2] = "User 2";

// All of the following will successfully retrieve the value for user1 ("User 1"):
// ONLY the first would work if allUsers was a Dictionary
trace(allUsers[user1]);
trace(allUsers[user1.toString()]); 
trace(allUsers["231049-1"]); 
trace(allUsers[new User(231049, true)]);