Flash CS3 Documentation |
|||
| Learning ActionScript 2.0 in Adobe Flash > Syntax and Language Fundamentals > About arrays > Using arrays > 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 About referencing and finding length, 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.
For a sample source file, array.fla, that illustrates array manipulation using ActionScript, see the Flash Samples page at www.adobe.com/go/learn_fl_samples. Download and decompress the Samples zip file and navigate to the ActionScript2.0/Arrays folder to access the sample. The code in the sample creates an array and sorts, adds, and removes items of two List components.
Flash CS3
RSS feed | Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/9.0/main/00000720.html
Comments
jcohick said on Nov 8, 2007 at 12:09 PM :