Introduction to Arrays

By Blue_Chi | Flash | Beginner

There are different types of data containers in ActionScript, the most known type is variables. Arrays are just like variables; they are data containers, while you can store a single date of any type in a variable name, arrays prove to be more complex than variables in that they could be used to store more than one value of any date type that can be accessed and modified independently. Arrays are in essence 'lists' of items.

Arrays help make our content more organised and make logical sense to our programs by gathering items of the same type under a single category. They are widely used in so many programming languages and they are involved in all types of projects starting from naming days when creating calendar and all the way to gathering enemy ship data in a shooting game.

This tutorial will hold a basic introduction on three main points regarding arrays: (1) the creation of arrays, (2) accessing arrays, and finally (3) modifying arrays. This tutorial does not claim to tell you everything about arrays, but it will give you the required knowledge for you to start your own exploration on arrays in ActionScript.

As we have started earlier, arrays are in essence lists of items that can be accessed and modified independently. Items within an array are called 'elements'. Each element inside an array is allocated to a position within an array from which the element could be accessed or modified. In addition, each array has a 'length' which is the number of items contained in the array.

Starting off with the creation of arrays, there are two ways to create an array in ActionScript. The first is by using the array constructor function Array(). Creating an array using its constructor function requires using the new key word. Whatever elements you want to put in your list are to be placed between the brackets separated by a coma. In the following example we have given our array the name myMacromediaList and it contains four elements:

var myMacromediaList = new Array("Flash","Dreamweaver","Freehand","Fireworks");

The second way to create an array in ActionScript is by using the [] operator. This method is shorter than the previous; you simply follow the equal sign with your elements contained within the square brackets. The same previous example could be written using the [] operator this way.

var myMacromediaList = ["Flash","Dreamweaver","Freehand","Fireworks"];

The outcome of both different methods is exactly the same. You have created an array that contains four elements. You can access these elements inside the array independently, however, you can access an array directly by simply using the array identifier. Accessing an array directly returns a string that contains the whole list of items separated by a coma. For example, the following code will generate the list "Flash, Dreamweaver, Freehand, Fireworks" in the debug window when testing the movie:

trace (myMacromediaList);

However, using an array in this sense would rather be pointless if we have gathered our items the way we did earlier. It is possible to access each element on its own by identifying the element by its position in the array. The position of an element within an array is known as its 'index'. The index of elements within an array are zero relative, this means that the counting of the number starts from zero and not one. In other words, our first element's index is '0', the second's index is '1', the third's index is '2', and so forth. This could also be remembered by the basic equation [N-1] where N is the location of an element position of the element in an array.

It is possible to access elements within an array through the element's index. To access an item this way we use the [] operator. For example, if we want to access the item "Flash" in our previous list we do it this way:

trace (myMacromediaList[0]);

Furthermore, if we wanted to access the element "Fireworks", we know that its index is 3 and not 4.

trace (myMacromediaList[3]);

Testing the movie with any of the previous two examples would generate the name of the respective element. In addition to the ability to let us access an element this way, we can modify any element by following the square brackets with an equal sign. To modify a specific item within an array we must use the [] operator along with its index. For example, if we want to change our first element from "Flash" to "8ball" we would have to do it this way:

myMacromediaList[0] = "8ball";

We can use the same technique to add an extra item at the end of our already existing list, for example, lets add a fifth item to our list.

myMacromediaList[4] = "Director";

The number of elements in an array is described as its length. As seen from the previous example, we can add more items to an array at any time which means that the array will expand to contain all the elements in it. The length of an array could be retrieved by accessing the .length property. For example, if we use the following code to retrieve the length of our original array it should return the number 4.

trace (myMacromediaList.length);

Identifying the length of an array is of great practical importance in real life. Advance usage of arrays is outside the scope of this tutorial as the topics of our tutorial are already covered, I will leave you with two example of how to use the arrays, the first is for cycling through an array, this example uses a for-loop and the length property to examine the contents of an array one by one:

for (i=0; i < myMacromediaList.length; i++){
trace ("now examining: " + myMacromediaList[i]);
}

Another simple example, which could be readily used to generate random content taken from an array is this simple randomiser:

var sections = ["Competitions", "tutorials", "news"];
var randomSection = sections[Math.floor((Math.random()*sections.length))];

This concludes our tutorial, we hope that you have learned how to create your own arrays, how to access the elements in your array and how to modify these elements. There are much more advanced methods readily available for you to manipulate, merge and split arrays which could be easily viewed from the ActionScript documentation. For more info please visit the Oman3D Forum.