开发者

as3 arraycollection

I am doing an pure AS3 programming, and I have urge to work with something like ArrayList because I need an array of objects (which in this case, array class is no use). However it seems there is no ArrayList class for me to import (Fl开发者_运维百科ex has but ...) I also found an open-source project called as3-collection on internet, but what the hell??? cant find any link to download...


Arrays work just fine with objects, but if you want strict enforcement of datatypes, try the Vector class.


var yourArray:Array;
var yourObject:Object;

...

yourArray = [];

yourArray.push(yourObject);

Is it something like that? Maybe I don't understand the question...


If you really need the methods of the ArrayList you could have a shot at creating your own implementation.

ArrayList is mainly a bunch of methods with an Array as the backend, it simplifies interaction with the Array but doesn't seem to do things that you wouldn't be able to do with an Array.

If you're looking for easier manipulation of data, create your own class with the necessary methods.


You seem to be looking for ArrayCollection.

If you'd tell me what you want to do with your array of objects then I could give you a more concise direction to how and what to do. It shouldn't be a problem.


I agree with @Robusto:

var ObjectsLenght:uint=10;
var vectorOfObjects:Vector.<Object> = new Vector.<Object>();
for (var i:uint=0 ;i<ObjectsLenght;i++){
    var o:Object = new Object();
    vectorOfObjects.push(o);
};
for (var j:uint=0 ;j<ObjectsLenght;j++){
    trace("Object["+j+"] = " + vectorOfObjects[j]);
};

Try to be more Specific, if you use a class of your choice...

For example MyClass.as:

package  {
    public class MyClass {
        var id:String;
        public function MyClass(id:uint) {
            this.id = "instance_of MyClass_" + id.toString()
        }
        public function getID():String{
            return this.id;
        }
    }
}

You're now able to create a Vector with MyClass as this :

import MyClass;
var instancesLenght:uint=10;
var vectorOfMyClass:Vector.<MyClass> = new Vector.<MyClass>();
for (var n:uint=0 ;n<instancesLenght;n++){
    var instance:MyClass = new MyClass(n);
    vectorOfMyClass.push(instance);
};
for (var m:uint=0 ;m<ObjectsLenght;m++){
    trace("MyClass["+m+"] = " + vectorOfMyClass[m] + " (" + vectorOfMyClass[m].getID() + ")");
};

As long as a Class extends the Object class, and you want change dynamically some properties, just declare your Class as dynamic like this.

package  {
    public dynamic class MyClass {
        var id:String;
        public function MyClass(id:uint) {
            this.id = "instance_of MyClass_" + id.toString()
        }
        public function getID():String{
            return this.id;
        }
    }
}

and then you may do this :

import MyClass;
var instancesLenght:uint=10;
var vectorOfMyClass:Vector.<MyClass> = new Vector.<MyClass>();
for (var n:uint=0 ;n<instancesLenght;n++){
    var instance:MyClass = new MyClass(n);
    instance.nameOfInstance = "ins_" + n.toString();
    vectorOfMyClass.push(instance);
};
for (var m:uint=0 ;m<ObjectsLenght;m++){
    trace("MyClass["+m+"] = " + vectorOfMyClass[m] + " (" + vectorOfMyClass[m].getID() + ")" + "name = " + vectorOfMyClass[m].nameOfInstance);
};

In the first case, you'll get this output :

Object[0] = [object Object]
Object[1] = [object Object]
Object[2] = [object Object]
Object[3] = [object Object]
Object[4] = [object Object]
Object[5] = [object Object]
Object[6] = [object Object]
Object[7] = [object Object]
Object[8] = [object Object]
Object[9] = [object Object]

In the second case this one :

MyClass[0] = [object MyClass] (instance_of MyClass_0)name = ins_0
MyClass[1] = [object MyClass] (instance_of MyClass_1)name = ins_1
MyClass[2] = [object MyClass] (instance_of MyClass_2)name = ins_2
MyClass[3] = [object MyClass] (instance_of MyClass_3)name = ins_3
MyClass[4] = [object MyClass] (instance_of MyClass_4)name = ins_4
MyClass[5] = [object MyClass] (instance_of MyClass_5)name = ins_5
MyClass[6] = [object MyClass] (instance_of MyClass_6)name = ins_6
MyClass[7] = [object MyClass] (instance_of MyClass_7)name = ins_7
MyClass[8] = [object MyClass] (instance_of MyClass_8)name = ins_8
MyClass[9] = [object MyClass] (instance_of MyClass_9)name = ins_9

I hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜