RSS link icon

.

Work with Arrays in Actionscript 3


In this lesson we will talk about making and using arrays with actionscript 3. Arrays is a HUGE topic, so I have decided to split it into 2 or 3 articles (not sure yet, but we will see).

In this first lesson we will talk about what arrays are, how to use them, and make our own simple one dimensional array.

In the next tutorial we will add more advanced theory to the arrays, such as multi dimensional arrays, making objects and adding properties to the array objects to make the arrays hold much more information then just a flat 1 dimensional array.

What is an array?

An array is kind of a data type, like a string, variable and so on, which means its purpose in life is to contain data. An array is usually made to contain more then one information, like a list of information. We could load a list of image url''s into and array like so [img1.jpg] [img2.jpg] [img3.jpg] [img4.jpg].

One thing I use arrays to a lot is to hold data sets from a database, or an xml file, so I can close my database connection after the data is loaded, and only needs to call the array data when needed. Its much easier and much faster, I will get into that in one of the more advanced array lessons.

How to make and use an array?

Now here is how to declare a simple array list, and put some simple one dimensional data into it.

This is the most simple form of an array, here is an example.

var myArray:Array = new Array("a", "b", "c");
trace(myArray[0]);
trace(myArray[1]);
trace(myArray[2]);

What we did here was to declare a variable with the name "myArray" and give it 3 data items, a, b and c.

You may have noticed how easy it is to trace an array item just by calling the myArray[number].

If you think this was simple, just wait to see how simple it is to add items and much more.

myArray.push("d");

That''s it, now lets bring it all together, lets try to add a couple of items to the list, then output all the items with a loop, we will count how many items the array holds, then loop through each item.

var myArray:Array = new Array("a", "b", "c");
myArray.push("d");
myArray.push("e");
myArray.push("f");

var i:int;
for (i = 0; i < (myArray.length); i++) {
  trace(myArray[i]);
}

Didn''t that look easy? and it is, this will output all letters from a to f.

The final thing I want to show you with simple arrays is how to remove an item from an array list.

myArray.splice(0, 1);

You might think its just as using the push method, but with removing we have the opportunity to remove a row of items at once, say we want to remove the first 3 items it would be.

myArray.splice(0, 2);

I could go on and on and on about using arrays to loop through and do a lot of stuff, and I assure you, I will do some more in depth lessons on that, but an article must have a limit of size, so for a complete list of methods for the array I can only recommend Adobe''s own livedocs its some dry reading, but its good for quick reference.


Manoj says: 2010-02-11

Please if anybody can help me to display an sorted heap algorithm once we entered values to an array...Sorted array values should be displayed in the algorithms nodes.

make money with photoshop - book