Table of Contents

11.3 Creating Arrays

We can create a new array with a data literal (i.e., by simply specifying its elements in a comma-separated list enclosed in brackets) or with the built-in array constructor function, Array( ).

11.3.1 Array Literals

Usually, it's more convenient to create arrays using array literals than to create them with the Array( ) constructor. Recall that a literal is a direct representation of a piece of fixed data. For example:

"beaver"      // A string literal
234.2034      // A numeric literal
true          // A boolean literal

In an array literal, square brackets demarcate the beginning and end of the array. Inside the square brackets, the values of the array's elements are specified as a comma-separated list. Here's the general syntax:

[expression1, expression2, expression3]

The expressions are resolved and then stored as the elements of the array being defined. Any valid expression can be used, including function calls, variables, literals, and even other arrays (an array within an array is called a nested array or a two-dimensional array).

Here are a few examples:

[4, 5, 63];                              // Simple numeric elements
["jeremy", "janice", "eman"]             // Simple string elements
[1, 4, 6 + 10]                           // Numeric expressions with an operation
[firstName, lastName, "tall", "skinny"]  // Variables and strings as elements
["month end days", [31, 30, 28]]         // A nested array literal

For comparison, let's do the same thing with the Array( ) constructor:

new Array(4, 5, 63)
new Array("jeremy", "janice", "eman")
new Array(1, 4, 6 + 10)
new Array(firstName, lastName, "tall", "skinny")
new Array("month end days", new Array(31, 30, 28))

As shown in the preceding example and noted in the Arrays in Other Programming Languages sidebar, elements of a single ActionScript array can contain data of different types.

11.3.2 The Array Constructor

To create an array with the Array( ) constructor, we use the new operator, followed by the word Array, followed by parentheses, which yields an empty array (one with no elements). As we'll see in Chapter 12, constructor functions create a new data object, in this case an Array instance. We normally assign a newly created array to a variable or other data container for future reference. For example:

var myList = new Array();  // Store an empty array in variable myList

We often want to assign initial values to an array's elements. We can do so by passing parameters to the Array( ) constructor when invoking it. Depending on the parameters we supply, the constructor invocation has different effects.

When we supply more than one argument to the Array( ) constructor, or when we supply a single nonnumeric argument to the Array( ) constructor, each argument becomes one of the element values in our new array. For example:

var frameLabels = new Array("intro", "section1", "section2", "home");

The array stored in frameLabels has the following elements:

 0: "intro"
 1: "section1"
 2: "section2"
 3: "home"

When we supply exactly one numeric argument to the Array( ) constructor, it creates an array with the specified number of empty placeholder elements:

var myList = new Array(14); // Creates an array with 14 empty elements

Arguments passed to the Array( ) constructor can be any legal expression, including compound expressions:

var x = 10;
var y = 5;
var myNumbers = new Array(x + 1, x * y, Math.random( ));

The myNumbers variable stores an array with the following elements:

 0: 11
 1: 50
 2: a floating-point number between 0 and 1

We mention the Array( ) constructor here because it can create an array with placeholder elements, which is more cumbersome to achieve with an array literal. Furthermore, it serves as a gentle introduction to constructor functions. In practice, you'll usually create your arrays using array literals or by importing them from an external source. The most common use for the Array( ) constructor is to create an empty array (possibly with a given number of placeholder elements).


Table of Contents