About adding and removing elements
An array contains elements and each element has a numeric position (the index), which is
how you refer to each element's position in the array. Each element can either hold a piece of
data or be empty. An element can hold the following data: a number, string, Boolean, or even
an array or object.
When you create elements in an array, you should create the indexes sequentially whenever
possible. This helps you when you debug your applications. In
length" on page
167, you saw that if you assign a single value in an array at index 5, the array
length returns as 6. This causes five undefined values to be inserted into the array.
The following example demonstrates how to create a new array, delete an item at a particular
index, and add and replace data at an index in an array:
var monthArr:Array = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
delete monthArr[5];
trace(monthArr); // Jan,Feb,Mar,Apr,May,undefined,Jul,Aug,Sep,Oct,Nov,Dec
trace(monthArr.length); // 12
monthArr[5] = "JUN";
trace(monthArr); // Jan,Feb,Mar,Apr,May,JUN,Jul,Aug,Sep,Oct,Nov,Dec
Even though you deleted the item at array index 5, the array length is still 12, and the item at
array index 5 changed to a blank string instead of disappearing completely.
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.
Creating indexed arrays
Indexed arrays store a series of one or more values. You can look up items by their position in
the array, which you might have done in earlier sections. The first index is always the number
, and the index increments by one for each subsequent element that you add to the array.
0
You can create an indexed array by calling the Array class constructor or by initializing the
array with an array literal. You create arrays using the Array constructor and an array literal in
the next example.
168
Syntax and Language Fundamentals
"About referencing and finding
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?