Recipe 5.9. Storing Complex or Multidimensional Data


Problem

You have two or more sets of related data and you want to be able to keep track of the relationships between their elements.

Solution

Use parallel arrays, an array of arrays (a multidimensional array), or an array of objects.

Discussion

You can create two or more parallel arrays in which the elements with the same index in each array are related. For example, the beginGradientFill() method, discussed in Chapter 7, uses three parallel arrays for the colors, alphas, and ratios of the values used in the gradient. In each array, the elements with the same index correspond to one another.

To create parallel arrays, populate multiple arrays such that the elements with the same index correspond to one another. When you use parallel arrays, you can easily retrieve related data, since the indexes are the same across the arrays; for example:

var colors:Array = ["maroon", "beige",    "blue",     "gray"]; var years:Array  = [1997,     2000,       1985,       1983]; var makes:Array  = ["Honda",  "Chrysler", "Mercedes", "Fiat"];       // Loop through the arrays. Since each array is the same  // length, you can use the length property of any of them  // in the for statement. Here, we use makes.length. for (var i:int = 0; i < makes.length; i++) {     // Displays:     // A maroon 1997 Honda     // A beige 2000 Chrysler     // A blue 1985 Mercedes     // A gray 1983 Fiat           // Display the elements with corresponding indexes      // from the arrays.     trace("A " + colors[i] + " " +                   years[i] + " " +                   makes[i]); }

Be careful when manipulating parallel arrays. If you add or remove elements from the arrays, you have to be certain to add or remove related data at the same position in every array. Otherwise the arrays will be out of sync and useless.


Another option for working with multiple sets of data is to create a multidimensional array, which is an array of arrays (i.e., an array in which each element is another array):

// Create an array, cars, and populate it with elements that  // are arrays. Each element array represents a car and  // contains three elements (color, year, and make). var cars:Array = new Array(); cars.push(["maroon", 1997, "Honda"]); cars.push(["beige", 2000, "Chrysler"]); cars.push(["blue", 1985, "Mercedes"]); cars.push(["gray", 1983, "Fiat"]);       // Loop through the elements of the cars array. for (var i:int = 0; i < cars.length; i++) {     // The output is the same as in the      // earlier parallel arrays example:     // A maroon 1997 Honda     // A beige 2000 Chrysler     // A blue 1985 Mercedes     // A gray 1983 Fiat           // Output each element of each subarray, cars[i].      // Note the use of two successive indexes in brackets,      // such as cars[i][0].     TRace("A " + cars[i][0] + " " +                   cars[i][1] + " " +                   cars[i][2]); }

The following is another way to view the two-dimensional cars arrays' contents. This displays the elements in a long list (the formatting isn't as nice as in the previous example, but it shows the array structure more clearly):

// Loop through the elements of the cars array. for (var i:int = 0; i < cars.length; i++) {     // Loop through the elements of each subarray, cars[i].     for (var j:int = 0; j < cars[i].length; j++) {         // Note the use of two successive indexes in brackets,         // cars[i][j].         TRace("Element [" + i + "][" + j + "] contains: " +                cars[i][j]);     } }

In the preceding example (the array of arrays), it is hard to discern the meaning of something like cars[i][0] or cars[i][j]. Furthermore, if the order of elements in a subarray changes, you would have to modify the code (or it might erroneously display "A Honda maroon 1997" instead of "A maroon 1997 Honda").

One alternative is to work with related data using an array of objects (associative arrays). This technique is similar to working with an array of arrays, but it offers the advantage of named properties. When you use an array of arrays, you must reference each value by its numbered index. However, when you use an array of objects, you can reference the data by property name instead of its index number. You can specify the properties of the object in any order you like because you'll refer to them later by name, not by number:

// Create an array, cars, and populate it with objects. // Each object has a make property, a year property,  // and a color property. var cars:Array = new Array(); // Here, object literals are used to define three properties // for each car; the object literals are added to  // the main array. cars.push({make: "Honda",    year: 1997, color: "maroon"}); cars.push({make: "Chrysler", year: 2000, color: "beige"}); cars.push({make: "Mercedes", year: 1985, color: "blue"}); cars.push({make: "Fiat",     year: 1983, color: "gray"});       // Loop through the cars array. for (var i:int = 0; i < cars.length; i++) {     // The output is the same as in the earlier examples,     // but each value is referenced by its property name,     // which is more programmer-friendly.     trace("A " + cars[i].color + " " +                   cars[i].year + " " +                   cars[i].make); }

See Also

Recipe 5.15 covers associative arrays, in which elements are accessed by name insteadof number.




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net