Actionscript 3 Lists and Data Structures

Actionscript 3 has quite a choice of different ways of grouping similar objects together in lists, being able to store data this way and iterate through it is one of the most important concepts in programming in any language. Choosing the correct data structure could make the difference in speed or in being able to access it easily at a later stage. There are a number of data structures and lists available they are Objects, Arrays, Vectors, Dictionaries and lastly ByteArrays. So what are each of these and how can we best utilise them? Objects At the heart of actionscript is objects it is the building blocks for the language and almost everything is a basic object. Objects are particularly useful if you have been used to using arrays because you can store variables which are indexed by a string rather than an integer. Heres an example of making use of an object to store some data.

var obj:Object = new Object(); = "bar";
var newobj:Object = new Object();
newobj["firstobject"] = obj;

If you had multiple objects you can iterate through this by using a for in loop heres an example

var objList:Object = new Object();
objList["foo"] = "bar";
objList["bar"] = "foo";
for(var x:String in objList)
    	//should output bar and then foo

Arrays Probably the most used know and used type is Arrays, like the objects we can create a list of variables but they are only indexed by an integer; If you have used any other language you should be familiar with this but heres an example of using arrays.

var myarray:Array = new array();
//outputs the foobar
//to loop through use a for loop
for(var i:int = 0; i < myarray.length(); i++)
    	//outputs foobar,foo,bar

Vectors Vectors are something relatively new to me, i have seen reference to them in c++ programming but haven’t actually made full use of them in actionscript. A vector is basically a typed array meaning that everything in the list must be of the same type. Heres an example of the vector type

//this will create 10 sprites in the list
var spList:Vector. = new Vector. ();
for(var i:int = 0; i < 10; i++)
    	var Sp:Sprite = new Sprite();

Dictionaries These are very similar to objects except with a little extra functionality, firstly being able to use anything to reference the variable not just a string and secondly it has a very useful parameter of being able to use weakkeys which basically helps with removing unused items from the garbage collection freeing up memory. For more information about memory and the garbage collector i strongly suggest you checkout his insights into the actionscript garbage collection process and how to handle memory has really kept me on track. Heres an example of using a sprite reference to store a sprite;

//the true enables the weak keys
var spList:Dictionary = new Dictionary(true);
var sp:Sprite = new Sprite();
spList[sp] = sp;
//accessing the data in a dictionary you can use a similar approach to the object
for each(var sp:Sprite in spList)

ByteArrays This is a great data type and i think is misunderstood by some but it basically allows you to manipulate and store data in a raw bytes. For example you could use this to generate sounds, images and much more. I personally have used it with amfphp to transfer images to flash. If you would like to see what can be done with this and learn more about it visit to see the latest actionscript bytearray projects and experiments. Which one? Ok so you’ve seen the different ways of storing data but which should you use and when? Well the truth is it really depends…each has its own use and benefits but heres a brief sentence about each to help you decide. Objects should be used when you need an easy way to access items using meaningful strings as keys and you do not care about the order of the data. Arrays should be used when your items need to be sorted or if the order of the items matters and when all the items in the list are not of the same type. Vectors should be used when you need the same functionality of arrays but all the data is of the same type. Switching over to a vector gives you a little performance boost so you should try to use this over arrays when you can. Dictionaries are a better replacements than objects are when you need a meaningful index or key to an object and also has the great benefit of allowing it to have a weak reference. ByteArrays should be used whenever you have raw binary data to store infact this is the only way of storing this data in actionscript 3.


    Comments are currently closed