O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Multidimensional Arrays

So far, we've limited our discussion to one-dimensional arrays, which are akin to a single row or a single column in a spreadsheet. But what if we want to create the equivalent of a spreadsheet with both rows and columns? We need a second dimension. ActionScript natively supports only one-dimensional arrays, but we can simulate a multidimensional array by creating arrays within arrays. That is, we can create an array that contains elements that are themselves arrays (sometimes called nested arrays).

The simplest type of multidimensional array is a two-dimensional array, in which elements are organized conceptually into a grid of rows and columns; the rows are the first dimension of the array, and the columns are the second.

Using a practical example, let's consider how a two-dimensional array works. Suppose we're processing an order that contains three products, each with a quantity and a price. We want to simulate a spreadsheet with three rows (one for each product) and two columns (one for the quantity and one for the price). We create a separate array for each row, with each row's elements representing the values in each column:

var row1:Array = [6, 2.99];   // Quantity 6, Price 2.99
var row2:Array = [4, 9.99];   // Quantity 4, Price 9.99
var row3:Array = [1, 59.99];  // Quantity 1, Price 59.99

Next, we place the rows into a container array named spreadsheet:

var spreadsheet:Array = [row1, row2, row3];

Now we can find the total cost of the order by multiplying the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required