Main Content

You are here:

Arrays (Indexed)

Hello and welcome. Today we shall be learning about one concept of ActionScript, arrays. Arrays are used for storing groups of information. Essentially, an array is a list of items. Those items can be text, or numbers, or in fact anything (see basic variables for examples).

From here on in, when I say "array", I mean indexed arrays, which means that everything in the array is given a number. The other type of array will be listed in a separate tutorial. Arrays in ActionScript (like lists) start with something. In ActionScript, however, these are given the number "0", not "1".

Array Creation

Supposing we wanted to create a simple array of day names, well this is how we would do it. First we need to declare the array, just like a variable (in fact, Arrays are technical from the same family as variables and objects as they are a complex data type [see variables] ). We do this like so:

var myArr:Array = new Array();

And the syntax for that is just like this:

var array_name:Array = new Array();

However, the array we now have is empty, and we need to fill it with stuff.

Populating (Filling in) the Array

If you want to write to an array, you just use [ ] and the number of what you wanted to set, like so:

myArr[0] = "Monday";
myArr[1] = "Tuesday";
myArr[2] = "Wednesday";
myArr[3] = "Thursday";

array_name[number] = something;

The number in the square brackets is known as the index (plural: indices). Each 'thing' in an array is known as an element.

Reading from an Array

This is just like writing to an array, but you don't have the equals sign.

trace(myArr[0]);//The first element of the array

Working with Random Indices

This is a perfectly valid code, and if you try it...

var myArr:Array = new Array(); 
myArr[24] = "Monday";
trace(myArr[24]);

...it would work. But what has happened to elements 0, 1, 2 etc? Well, the array now has 25 elements, not just one, in it - but 0, 1, 2, 3, ... 23 are empty. They exist, but they don't hold anything at all.

Integrating Loops with Arrays

Loops can easily be integrated with arrays. The only thing you have may want to know is the length property of the array. You can get this length by using this:

myArr.length

For loops are particularly good, because they can take this length and use it to do something with everything in an array. This is an example of a message writer, though you can do so much more, as demonstrated in some of our tutorials:

var myArr:Array = new Array();
myArr[0] = "H";
myArr[1] = "e";
myArr[2] = "l";
myArr[3] = "l";
myArr[4] = "o";
for (var i = 0; i < myArr.length; i++) {
        trace(myArr[i]);
}

Shorthand Array Creation

The chances are, when you come to creating an array, you'll probably already know what you're going to put in it. If you do, you can save yourself a fair bit of time. These are two shorthand examples of what I showed you earlier:

var myArr:Array = new Array("Monday","Tuesday","Wednesday","Thursday");
var myArr:Array = ["Monday","Tuesday","Wednesday","Thursday"];

Note that they is no "new Array()" is the latter, but there is in the former. As you can see, it's significantly shorter, but offers you a bit less control.

That concludes this tutorial on arrays, which you will undoubtedly need to use in future.

Harry.

Comments