Array (Object)
Top  Previous  Next


An Array is an Object that is composed of a number of elements. An element can be any value (eg string) or Object. The types of the elements do not have to be all the same, but usually they will be. The elements are access using an index. The index is usually a numeric value starting from zero for the first element. The index is specified by a value within square brackets after an array variable name. The square brackets are called the array access operator.

SWiSH Player Support
Supported Internally

Sample
dayArray = new Array;
dayArray[0
] = "Monday";
dayArray[1
] = "Tuesday";
dayArray[2
] = "Wednesday";
dayArray[3
] = "Thursday";

Arrays are a convenient way of handling tables of data. Access to the specific element is via an integer, which could be another variable. This property makes an array an extremely powerful tool, especially when used with looping constructs such as while, do while etc.

The first 3 days of the week could be conveniently traced with the following code:

while (i < 3{
    trace(dayArray[i++]);
}


Creating Arrays
Arrays can be created in any of the following ways:

name = new Array;  
name[index] = "value";  
name = new Array(length);             (Note: won't work with SWF4 Movies)  
name = new Array(value1, value2...);          (Note: won't work with SWF4 Movies)  

For SWF5+ you need to create an array Object and assign it to a variable BEFORE you can start accessing or setting the variable as an array. Therefore simply going name[index] = "value"; won't work. For this reasons the RECOMMENDED WAY to do arrays that are compatible for both SWF4 and SWF5+ is:

name = new Array;  

Flash MX Differences
The SWiShscript array object only support the MX array object methods when you specify SWF5 or higher. For SWF4, no array methods or properties are supported. Also the array constants and and special constructors are only supported for SWF5 or higher. In addition, using the 'new Array' as shown in the above example is not strictly required for SWF4.