# Advanced Nested Arrays

 Earlier in this chapter, we briefly discussed nested arrays (arrays held within other arrays). Now we are going to discuss some advantages to using these nested arrays. First, let's go over again how to create one. The example we'll use here involves the starting five of a basketball team by position. This example shows the following information: Points scoredShots takenTotal rebounds We will start with just the first two positions and combine them, as shown here: ` var pG:Array = new Array(12,15,4); var sG:Array = new Array(20,22,5); var team:Array = new Array(pG,sG); trace(team); //output: 12,15,4,20,22,5 ` Now that we have the data entered in, we could get the point guard's rebounds from the team array, without showing the other elements. To do this, we call the index of an indexed element. This may sound complicated, but it's not. We want to know how many rebounds the point guard has (the third element in the first element of the team array). Here's the code we'll use: ` var pG:Array = new Array(12,15,4); var sG:Array = new Array(20,22,5); var team:Array = new Array(pG,sG); trace(team[0][2]); //output: 4 ` Success! We retrieved an individual element from a nested array. This is a very powerful tool when you have massive arrays with many nested arrays. Now let's take this a step further. We'll add the rest of the team and this time get the total for each category and place this information in an array called totals. We'll also divide the totals, as they are being calculated, by the main array's length property to get the averages for the players and then place that information into another array called averages. Here's the code: ` //First, get all the players ready with their stats in their own array var pG:Array = new Array(12,15,4); var sG:Array = new Array(20,22,5); var sF:Array = new Array(11,13,8); var pF:Array = new Array(18,14,16); var c:Array = new Array(20,17,21); //Now combine all the players arrays into one array called "team" var team:Array = new Array(pG,sG,sF,pF,c); var totals:Array = new Array(); var averages:Array = new Array(); //Now lets create the loop statement that will perform all the necessary //tasks we want for(var i:Number = 0; i

Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319

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