Developer Guide
Associative array with an indexed array
To make the individual arrays easier to access, you can use an associative array for the days of the week and an
indexed array for the task lists. Using an associative array allows you to use dot syntax when referring to a particular
day of the week, but at the cost of extra run-time processing to access each element of the associative array. The
following example uses an associative array as the basis of a task list, with a key and value pair for each day of the
var masterTaskList:Object = new Object();
masterTaskList["Monday"] = ["wash dishes", "take out trash"];
masterTaskList["Tuesday"] = ["wash dishes", "pay bills"];
masterTaskList["Wednesday"] = ["wash dishes", "dentist", "wash dog"];
masterTaskList["Thursday"] = ["wash dishes"];
masterTaskList["Friday"] = ["wash dishes", "clean house"];
masterTaskList["Saturday"] = ["wash dishes", "wash car", "pay rent"];
masterTaskList["Sunday"] = ["mow lawn", "fix chair"];
Dot syntax makes the code more readable by making it possible to avoid multiple sets of brackets.
trace(masterTaskList.Wednesday[1]); // output: dentist
trace(masterTaskList.Sunday[0]);// output: mow lawn
You can iterate through the task list using a loop, but you must use bracket notation instead of dot syntax
to access the value associated with each key. Because
masterTaskList is an associative array, the elements are not
necessarily retrieved in the order that you may expect, as the following example shows:
for (var day:String in masterTaskList)
trace(day + ": " + masterTaskList[day])
/* output:
Sunday: mow lawn,fix chair
Wednesday: wash dishes,dentist,wash dog
Friday: wash dishes,clean house
Thursday: wash dishes
Monday: wash dishes,take out trash
Saturday: wash dishes,wash car,pay rent
Tuesday: wash dishes,pay bills
Cloning arrays
The Array class has no built-in method for making copies of arrays. You can create a shallow copy of an array by
calling either the
concat() or slice() methods with no arguments. In a shallow copy, if the original array has
elements that are objects, only the references to the objects are copied rather than the objects themselves. The copy
points to the same objects as the original does. Any changes made to the objects are reflected in both arrays.
In a deep copy, any objects found in the original array are also copied so that the new array does not point to the same
objects as does the original array. Deep copying requires more than one line of code, which usually calls for the
creation of a function. Such a function could be created as a global utility function or as a method of an Array
The following example defines a function named
clone() that does deep copying. The algorithm is borrowed from
a common Java programming technique. The function creates a deep copy by serializing the array into an instance
of the ByteArray class, and then reading the array back into a new array. This function accepts an object so that it
can be used with both indexed arrays and associative arrays, as shown in the following code:

Get ADOBE® FLEX® 3: PROGRAMMING ACTIONSCRIPT™ 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.