What Are Arrays?

[ LiB ]

When you learned how to create variables , what you were essentially doing was creating a storage location for a piece of data. Arrays are a collection of variables that can hold individual pieces of data. Most of the time, these pieces of data are related , allowing you to reference them through one alias or name . Each of these elements has a unique position within the array. This is referred to as the index . Once you have your array, you can then access your data element by referencing it through your index and you're set.

Arrays can include more than one data type; what this means is that the first element in the array can be of type integer, the second could be type float, and the following type string, and so on. This can allow you to store an address, for instance. Along with this address you can also store the person's name and age as separate elements.

To better visualize this, check out Figure 5.1.

Figure 5.1. The difference between arrays and variables

graphic/05fig01.gif


So what is Figure 5.1 showing? It's demonstrating the fact that you can be more organized by storing your related data into an array variable instead of into many different variables.

You learned how to create your own functions and how to pass variables to those functions in the last chapter. You can actually pass your array variable to a function in one shot instead of passing many arguments at once. This reduces program overhead, thus reducing slowdown . To pass your array variable to a function, just pass on the array's name.

NOTE

NOTE

You'll see examples of all of these programming techniques in chapters to come. It would be a good idea to create short programs that verify all of what I'm telling you.This is one of the best ways to retain all the infor mation I'm throwing at you.

As another example of using arrays, let's say you had to print a list of names that you have collected over a period of time. This collection of names could be a high-score list. If you collected, say, 50 names in separate variables, imagine the mess you would have!

Let's add an array to the scene. You could easily create an array that will contain 50 elements that will store each of those names. Now what would you do if you wanted to display these names? The best solution would be to create a function that can loop through each element while posting it to the screen, in whatever manner you like.

Now, if you didn't have this array working with you, you would have to forget about that nice little loop. In order to display the information in those 50 variables you created, you would have to actually call the function 50 timesand that wouldn't be fun or practical.

[ LiB ]


Game Development with ActionScript
Game Development with ActionScript
ISBN: 1592001106
EAN: 2147483647
Year: 2004
Pages: 162
Authors: Lewis Moronta

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