About referencing and finding length
When you work with arrays, you often need to know how many items exist in the array. This
can be very useful when writing
execute a series of statements. You can see an example in the following snippet:
var monthArr:Array = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
trace(monthArr); // Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec
trace(monthArr.length); // 12
var i:Number;
for (i = 0; i < monthArr.length; i++) {
monthArr[i] = monthArr[i].toUpperCase();
}
trace(monthArr); // JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC
In the previous example, you create an array and populate it with month names. The contents
are displayed, and also the array's length. A
converts the value to uppercase, and the array contents are displayed again.
In the following ActionScript, if you create an element at array index 5 in an array, the length
of the array returns
6
the array as you might expect:
var myArr:Array = new Array();
myArr[5] = "five";
trace(myArr.length); // 6
trace(myArr); // undefined,undefined,undefined,undefined,undefined,five
For more information on
the array access operator, see
You can find a sample source file, array.fla, in the Samples folder on your hard disk. This
sample illustrates array manipulation using ActionScript. The code in the sample creates an
array and sorts, adds, and removes items of two List components. Find the sample file in the
following directories:
In Windows, browse to boot drive\Program Files\Macromedia\Flash 8\Samples and
Tutorials\Samples\ActionScript\Arrays.
On the Macintosh, browse to Macintosh HD/Applications/Macromedia Flash 8/Samples
and Tutorials/Samples/ActionScript/Arrays.
loops that iterate through every element in the array and
for
(because the array is zero based), and not the actual number of items in
loops, see
"Using for loops" on page
for
"Using dot and array access operators" on page
loop iterates over each item in the array and
for
157. For information on
184.
About arrays
167
Need help?
Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?