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
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
| 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
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. |
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
Figure 5.17:
Nested arrays are powerful tools. Here,
join
is called to print a set of nested arrays.
When you create a nested array, you are
Figure 5.18:
A one-dimensional array is just one long block of elements. A two-dimensional array is divided into rows and
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
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
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