About modifying arrays

You can also control and modify the array using ActionScript. You can move values around an array, or you can change the size of the array. For example, if you want to exchange data at two indexes in an array, you can use the following code:

var buildingArr:Array = new Array();
buildingArr[2] = "Accounting";
buildingArr[4] = "Engineering";
trace(buildingArr); // undefined,undefined,Accounting,undefined,Engineering

var temp_item:String = buildingArr[2];
buildingArr[2] = buildingArr[4];
buildingArr[4] = temp_item;
trace(buildingArr); // undefined,undefined,Engineering,undefined,Accounting

You might wonder why you need to create a temporary variable in the previous example. If you copied the contents of array index 4 into array index 2 and vice versa, the original contents of array index 2 would be lost. When you copy the value from one of the array indexes into a temporary variable, you can save the value and safely copy it back later in your code. For example, if you use the following code instead, you can see that the value of array index 2 (Accounting) has been lost. Now you have two engineering teams but no accountants.

// wrong way (no temporary variable)
buildingArr[2] = buildingArr[4];
buildingArr[4] = buildingArr[2];
trace(buildingArr); // undefined,undefined,Engineering,undefined,Engineering

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.