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 scored

  • Shots taken

  • Total 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<team[0].length; i++){    //reset the holders    var tempTotal:Number = 0;    var tempAvg:Number = 0;    for(var j:Number = 0; j<team.length; j++){         tempTotal += team[j][i]; //Place the total of each tempElement into the totals array          totals[i]=tempTotal; //Divide the total of each sub-element by //the main array's length to get the //average         tempAvg +=(team[j][i])/team.length;         averages[i] = tempAvg;    } } trace(totals); trace(averages); //output: 81, 81, 54 //        16.2,16.2,10.8 

In this example, we drew information in sequence from the nested arrays, totaled each column, and placed the totals in another array. We also successfully got the averages for all the players and placed them in another array. This is just one of the many possibilities for using this method.

Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319 © 2008-2017.
If you may any questions please contact us: