Monthly Archives: August 2006

Collections in ActionScript 3.0

As you can see from the title, this article is about Collections in ActionScript, from the wikipedia, we can get some info about collections:

In object-oriented programming, a collection class is any class that is capable of storing other objects. Collection classes usually implement some kind of data structure, such as a list, map, set, array, or tree. A collection class is usually able to store an arbitrary number of data items, i.e. the size of the collection is adjusted automatically.


The reason we have collections, is languages like Java, C, C++, C# in languages like this arrays are very poor objects to hold data, they have limited size, which you choose when you initialize them, and arrays can hold only instances of very same data type. So coders invented collections. Collections could hold different datatypes, they didn’t have any problems with size and so on and so forth. So you may want to ask me:
“Why do we need collections? we have very nice arrays.”
And i will answer this:
“Because they can make our lives even more comfortable”

In our case, arrays can hold any data types, and their size is dynamic, but this is not all, so that’s why we dont really need collections, also arrays have bunch of methods for sorting and filtering, but there are some cases when we might want to use other types of collections, while coding in ActionScript.

Flash Player API, the core stuff

Here we have 4 classes which were designes as collections: Array, ByteArray, Dictionary, XMLList, you can also use Object, XML, XMLDocument and some other classes, but i would not recommend it.


Array class have some tasty modifications sience ActionScript version 2.0, now we have cool methods for itteration: every, filter, forEach, map and some. I wont tell you about sort and sortOn methods, because they are quite old, and you can find alot of articles about them in i-net, instead lets see at new methods:

Every method has two parameters: callback:Function – reference to the filter method, which ussualy should return boolean, and thisObject:Object – reference to instance which we use as this in callback.

every() Method

Returns: Boolean
This method invokes callback on every object, while method returns true, when you get first false, method stopes to itterate. With this method we be sure that array meets our requirements.

filter() Method

Returns: Array
Method makes new array, if callback returns true, element is moved to new array.

forEach() Method

Returns: nothing :P
This method can replace for,for in, foreach in, it just runs callback on every element. (So that means now callback can return void).

map() Method

Returns: Array
This method is something like filter and forEach, method runs callback on every item in array, and creates new one, in callback method we “tell” how do we want to change item before moving it to new place.

some() Method

Returns: Boolean.
This is opposite to the every() method, callback being run on every item, while return value is false, when true is returned method stopes.

That’s all about Array’s new itteration method, i think that we can use arrays when we have to modify, add, remove, filter, sort items but what should we do in other cases?


This is very interesting class, it made some revolution in our ActionScript world, with bunch of new classes such as URLStream, Socket we can operate with bytes now, we can send and load any kind of data, or even load just part of files (you can check ID3Loader component for example)

ByteArray class has some useful methods for reading and writing primitive data types and objects, for example readInt/writeInt,readChar/writeChar,readObject/writeObject etc. If you have to work with bytes or just need storage for massive ammount of primitive data types, ByteArray is your choice.


In ActionScript 2.0 when i needed something very simple to hold my data, I used pure Objects or their subclasses with bunch of methods, now we have more specialized class for this – flash.utils.Dictionary. I cannot say much about this class, becouse it has nothing but the ability to check types of data for identifiers, for example look at this code from Flex Builder documentation:

[ftf h="220"]var dict = new Dictionary();
var obj = new Object();
var key:Object = new Object();
key.toString = function() { return “key” }
dict[key] = “Letters”;
obj["key"] = “Letters”;
dict[key] == “Letters”; // true
obj["key"] == “Letters”; // true
obj[key] == “Letters”; // true because key == “key” returns true because key.toString == “key”
dict["key"] == “Letters”; // false ‘cose “key” === key returns false
delete dict[key]; //removes the key[/ftf]


We have four classes in ActionScript 3.0 which designed to work with XML data: XML,XMLList, flash.xml.XMLDocument, flash.xml.XMLNode. Let’s say that XMLNode is “utility” class for XMLDocument (and base class by the way). XMLDocument and XML classes are “code representation” of XML documents in ActionScript, they have root node, headers and other stuff valid documents has. Unlike them XMLList can hold pure nodes (XML documents, or parts of xml documents). So we can use XMLList class as collection, and it perfectly suits for tree based data.

Continue reading