Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

macromedia flash mx - Reinhardt R.

Reinhardt R., Lott J macromedia flash mx - John Wiley & Sons, 2004. - 987 p.
ISBN 0-7645-4354-7
Download (direct link): macromediaflash2004.pdf
Previous << 1 .. 144 145 146 147 148 149 < 150 > 151 152 153 154 155 156 .. 419 >> Next

var aEmployees:Array = new Array();
aEmployees.push("Arun:555-1234");
aEmployees.push("Peter:555-4321");
aEmployees.push("Chris:555-5678");
aEmployees.push("Heather:555-8765");
Then you could use String object methods to extract the names and the birthdays when you want to use them:
// The split() method separates string by specified // delimiter into a new array. var aEmployees:Array = new Array(); aEmployees.push("Arun:555-1234");
Chapter 11 ¶ Using the Array Class 303
aEmployees.push("Peter:555-4321"); aEmployees.push("Chris:555-5678"); aEmployees.push("Heather:555-8765"); var aTempEmployeeInfo:Array = null; for(var i:Number = 0; i < aEmployees.length; i++) { aTempEmployeelnfo = aEmployees[i].split(":"); trace("Employee:" + aTempEmployeeInfo[0]); trace("Phone Number:" + aTempEmployeeInfo[1]);
}
The preceding will result in the following display in the Output panel:
Employee:Arun
Phone Number:555-1234
Employee:Peter
Phone Number:555-4321
Employee:Chris
Phone Number:555-5678
Employee:Heather
Phone Number:555-8765
Although that works, it is somewhat overly complex when all you want to do is something as simple as store and retrieve a name and corresponding phone number. A much easier way to solve this problem is to use what are known as parallel arrays.
The idea behind parallel arrays is simply to create two (or more) arrays in which the elements with the same indices are related. So using the employee scenario, you could create two parallel arrays as follows:
var aEmployeeNames:Array = ["Arun", "Peter", "Chris", "Heather"];
var aEmployeePhone:Array = ["555-1234", "555-4321", "555-5678", "555-8765"];
Then it is much easier to retrieve the corresponding elements from each array than to try and parse through a string as you did earlier. All you need to do is to access the elements with the same index from each array:
var aEmployeeNames:Array = ["Arun", "Peter", "Chris", "Heather"]; var aEmployeePhone:Array = ["555-1234", "555-4321", "555-5678", "555-8765"]; for(var i:Number = 0; i < aEmployeeNames.length; i++) { trace("Employee:" + aEmployeeNames[i]); trace("Phone Number:" + aEmployeePhone[i]);
}
This displays the following in the Output panel:
Employee:Arun
Phone Number:555-1234
Employee:Peter
Phone Number:555-4321
Employee:Chris
Phone Number:555-5678
Employee:Heather
Phone Number:555-8765
Notice that this is the same output as before, but the code is simplified.
304
Part IV ¶ The Core Classes
You are not limited to using just two arrays with corresponding data. You can use as many as you need and can manage. Letís continue on with the same employee example and imagine that you want to add one more piece of information about each employee. Perhaps you want to add the number of years employed. You could then easily add a third parallel array:
var aEmployeeYears:Array = [5, 7, 3, 1];
Working with Multidimensional Arrays
You can think of the standard, single-dimension array as a single column of data. Many other languages support what are known as multidimensional arrays. You can think of a two-dimensional array, for example, as a grid where each element is determined by two indices ó a row and a column index. A three-dimensional array can be thought of as representing three-dimensional space, and each element is determined by three indices ó the row, column, and depth. In ActionScript, you can represent this construct using an array of arrays. Here is an example:
var aEmployees:Array = new Array(); aEmployees.push(["Arun", "555-1234"]); aEmployees.push(["Peter", "555-4321"]); aEmployees.push(["Chris", "555-5678"]); aEmployees.push(["Heather", "555-8765"]); for(var i:Number = 0; i < aEmployees.length; i++) { trace("Employee:" + aEmployees[i][0]); trace("Phone Number:" + aEmployees[i][1]);
}
The preceding code creates a new array, aEmployees, and appends to it four elements that are, themselves, arrays. Then, notice that each value is accessed using two indices. The first index specifies the row (the element of the outermost array), whereas the second index specifies the column (the element of the innermost arrays). The result is very similar to what you achieved using parallel arrays in the earlier section. In fact, parallel arrays and two-dimensional arrays can be used almost interchangeably.
Of course, you can create arrays as elements of arrays that are, themselves, elements of an array. Such a scenario would mimic a three-dimensional array. You can even create arrays of greater dimensions, although once you get beyond three or four dimensions, it can become confusing.
Working with Arrays of Objects
Another type of array that can be useful is an array of associative arrays. We discussed associative arrays in Chapter 7. As a quick refresher, however, an associative array is an object with named indices called keys. Arrays of associative arrays can be useful when you have a list of data in which each element is composed of various, named subelements. For example, this same employee/phone number example is a good candidate for this type of construct. Here is an example:
Previous << 1 .. 144 145 146 147 148 149 < 150 > 151 152 153 154 155 156 .. 419 >> Next