WHAT IS AN ARRAY?
To put it simple an array is a representation of a data structure.
Whatever data comes to your mind it can defined by an array : from a generic sequence of numbers to a contact list or a collection of records etc...
An example could be the list of days in the week, something like:
"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"
An array can be made up of primitive type values like strings, numeric values, booleans or complex type values like other arrays or objects.
THE CONSTRUCTOR In an object oriented environment like actionscript you should expect arrays to be objects themselves and this is exactly what is going on. If you have a little experience in ObjectOriented programming you should know that every object has to be constructed.
In actionscript we use the new keyword to create a new Array, look at this sample:
myArray = new Array()
otherArray = new Array("June","July","August","September")
There are 3 ways of constructing an array:
the first one will create an empty object while the second one will populate it with the passed parameter .
The third way looks like this:
myArray = new Array(16)
in this case we create an empty array that can contain 16 elements.
So passing one single numeric value to the constructor will create an array with n number of empty elements.
If you pass one or more numeric (or non-numeric) values these will populate the array with those values.
RETRIEVING DATA FROM ARRAYS All the elements in an array are indexed with a numeric value starting from 0.
If you go back to the example of the months :
otherArray = new Array("June","July","August","September")
the array is represented like this:
0: June
1: July
2: August
3: September
In other words we can retrieve each element like this:
trace( otherArray[0] ) // will display >> June
trace( otherArray[3] ) // will display >> September
the length property of the Array object tells us how many items are in one array
trace( otherArray.length ) // will display 4
Once we know this, it should be very ease to cycle trough all the array elements:
for (var i=0; i < otherArray.length; i++)
{
trace( otherArray[i] )
}
(continues on page 2) |