Flylib.com

Books Software

 
 
 

Initializing Arrays


Initializing Arrays

So far we've been populating our arrays by accessing the individual indexes and using the assignment operator, or by multiple calls to the push method. Both these ways are valid, but they are not the only way. You can set up your arrays using the constructor for the method. (Constructors are discussed in Chapter 6.) Consider the following example:

myArray = new Array("apple"," banana"," cabbage");
trace(myArray.toString());

In this example, the array gets three strings as its initial elements. These elements are given as arguments to the constructor for the Array object. For short arrays, this style of initialization can save time and screen space.

Another possible shortcut is as follows :

myArray = new Array();
myArray = ["apple"," banana"," cabbage"];
trace(myArray.toString());

This script isn't as elegant as the first one, but it's still worth mentioning. As you can see, the array is first created and then populated with the assignment to ["apple"," banana",cabbage"] .

Note  

By using the type of initialization used in the previous example, you can get away with omitting the line that creates the array. When Flash sees the myArray = [] part, it automatically creates a new array, so the preceding script could have simply been the following:


myArray = ["apple"," banana"," cab-


bage"];


trace(myArray.toString());

This is called an implicit instantiation of the Array object. The reason I don't like this style is because it does not explicitly state that myArray is an array. It relies on the reader of the code to know that the implicit instantiation creates an Array object.



Nested Arrays

What happens if you create an array and then populate each of its elements with other arrays? You would get a nested array. Consider the following example:

myArray = new Array();
for(i=0; i<3; ++i){
    myArray[i] = new Array();
    for(j=0; j<4; ++j){
        myArray[i].push("i"+i+" j"+j);
    }
}
trace(myArray.join(": "));

This example has some subtle things going on, so let's take a close look at it. First, a new array is created. Then a for loop is iterated, in which the first three elements of myArray are given new nested arrays. Another nested for loop is run, which creates four new elements in the nested array. The for loop then closes and another for loop is run. This final loop calls join , which prints the main array using a colon (:) as a separator. This has the effect of printing each nested array, separated by a colon . Because each element in the array is actually an array, Flash calls toString on each of the nested arrays. You end up with output like Figure 5.17. Notice how each nested array has its elements printed with comma separators but the main array has colons? That's because we used join and not toString on our main array.

click to expand
Figure 5.17: Nested arrays are powerful tools. Here, join is called to print a set of nested arrays.



Multidimensional Arrays

When you create a nested array, you are essentially creating a multidimensional array. Figure 5.18 shows a diagram of a single array and a multidimensional array, side by side.


Figure 5.18: A one-dimensional array is just one long block of elements. A two-dimensional array is divided into rows and columns .

The beauty of a multidimensional array is the indexing system it uses. By simply tacking an extra index to the end of the array, you can refer to any element of a multidimensional array. An array access on a two-dimensional array would have the following general form:

myArray[row][column];

As you can see from the general form and Figure 5.18, the first index indicates the row of the element to access, and the second index indicates the column. With that in mind, consider the following script:

myArray = new Array();
for(i=0; i<5; ++i){
    myArray[i] = new Array();
    for(j=0; j<5; ++j)
        myArray[i][j] = "["+i+","+j+"]";
}
for(i=0; i<5; ++i){
    myOutputString = "";
    for(j=0; j<5; ++j)
        myOutputString += myArray[i][j] + "";
    trace(myOutputString);
}

This script is designed to create a two-dimensional array of strings where each string is an (x,y) pair indicating the indexes of that element. The program outputs this array in a block form so that the output matches the actual array being printed, as seen in Figure 5.19. Notice that in both loops , we are accessing the individual elements with a double index (two sets of brackets).

click to expand
Figure 5.19: A multidimensional array can be used with a double index. The first index is the row and the second index is the column.

Arrays can have even more than two dimensions. They can, in theory, have any number of dimensions you like. Consider the following example of the three-dimensional array that is 3 — 3 — 5:

myArray = new Array();
for(i=0; i<3; ++i){
    myArray[i] = new Array();
    for(j=0; j<3; ++j){
        myArray[i][j] = new Array();
        for(k=0; k<5; ++k)
            myArray[i][j][k] = "("+i+","+j+","+k+")";
    }
}
for(i=0; i<3; ++i){
    for(j=0; j<3; ++j){
        myOutputString = "";
        for(k=0; k<5; ++k)
            myOutputString += myArray[i][j][k] + "";
        trace(myOutputString);
    }
    trace(""); //blank line
}

The output of this example can be seen in Figure 5.20. Notice the way that each index gets more local. In other words, the first index is the broadest, the second is a bit less broad, and the third is very specific. You could say the first index is the block number, the second is the row number, and the third is the column number. The only problem with that naming scheme is that you'll have to invent more names when you take arrays beyond three dimensions.

click to expand
Figure 5.20: An array can have as many dimensions as you like; this one has three. I divide them up into blocks, rows, and columns.

These multidimensional arrays are extremely important for us. Most board games make heavy use of them. As a result, you'll see countless examples of their use in this book. We're almost ready to get to this chapter's games , but first we need to talk about one final thing arrays can be used for in Flash.