11.5 Determining the Size of an Array

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 11.  Arrays

All arrays come with a built-in property named length, which indicates the current number of elements (including undefined elements). To access an array's length property, we use the dot operator, like so:

arrayName.length

Here are a few examples:

myList = [34, 45, 57]; trace(myList.length);             // Displays: 3 myWords = ["this", "that", "the other"]; trace(myWords.length);            // Displays: 3, the number of elements,                                    // not the number of words or characters frameLabels = new Array(24);      // Note the single numeric argument                                   // used with the Array( ) constructor trace(frameLabels.length);        // Displays: 24

The length of an array is always 1 greater than the index of its last element. For example, an array with elements at indexes 0, 1, and 2 has a length of 3. And an array with elements at indexes 0, 1, 2, and 50 has a length of 51. 51? Yes, 51. Even though indexes 3 through 49 are empty, they still contribute to the length of the array. The last element of an array is always myArray[myArray.length - 1], because index numbers begin at 0, not 1.

If we add and remove elements, the array's length property is updated to reflect our changes. In fact, we can even set the length property to add or remove elements at the end of an array. This is in contrast the string.length property, which is read-only, as discussed in Chapter 4. Shortening the length of an array deletes elements beyond the new length. The length property includes numbered elements only, not named elements, as described in Section 11.6.

Using an array's length property, we can create a loop that accesses all the elements of an array, as we saw in Example 8-1. Looping through an array's elements is a fundamental task in programming. To get a sense of what's possible when we combine loops and arrays, study Example 11-1, which hunts through a soundtracks array to find the location of the element with the value "hip hop". You should recognize the for loop from Chapter 8, and the increment operator from Chapter 5. As for the array-access code, well, you just finished learning about that.

Example 11-1. Searching an array
// Create an array var soundtracks = ["electronic", "hip hop", "pop", "alternative", "classical"]; // Check each element to see if it contains "hip hop" for (var i = 0; i < soundtracks.length; i++) {   trace("Now examining element: " + i);   if (soundtracks[i] =  = "hip hop") {     trace("The location of 'hip hop' is index: " + i);     break;   } }

Let's extend Example 11-1 into a generalized search function that can check any array for any matching element, as shown in Example 11-2. Our search function returns the position within the array where the element was found, or null if it was not found.

Example 11-2. A generalized array-searching function
function searchArray (whichArray, searchElement) {   // Check each element to see if it contains searchElement   for (var i = 0; i < whichArray.length; i++) {     if (whichArray[i] =  = searchElement) {       return i;     }   }   return null; }

Here's how you might make use of our new search function to check whether or not "Fritz" is one of the names in our userNames array, which is an array of authorized usernames:

if (searchArray(userNames, "Fritz") =  = null) {   trace("Sorry, that username wasn't found"); } else {   trace("Welcome to the game."); }

Now that's invigorating! This is one of those rewarding moments when all our hard work comes together in a single system. Seemingly trivial individual operations can combine to form extremely powerful and flexible programs. Like the letters in the alphabet or the sequence of base-pairs in a strand of DNA, you can construct anything imaginable from the simple building blocks at your disposal. The remainder of this chapter explains more about the mechanics of manipulating arrays, including the use of built-in functions that already perform some common functions for you. As you encounter needs not met by the built-in functions, consider writing your own custom functions, such as the searchArray( ) function in the preceding example.

     



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

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