11.3. Creating ArraysWe can create a new array with a data literal (i.e., simply typing out all the elements) or with the special built-in array constructor function, Array( ). 11.3.1. The Array ConstructorTo create an array with the Array( ) constructor, we use the new operator followed by the Array keyword followed by parentheses, which yields an empty array (one with no elements). 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 would have 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 would thus store an array with the following elements: 0: 11 1: 50 2: a floating-point number between 0 and 1 11.3.2. Array LiteralsSometimes, 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 fixed 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 signal the beginning and end of the array. Inside the square brackets, a comma-separated list of expressions provides the values of the array's elements. Here's the general syntax: [expression1, expression2, expression3] The expressions are resolved and then stored as the elements of the array being described. Any valid expression may be used, including function calls, variables, literals, and even other arrays (an array within an array is called a nested 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 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)) Notice that the elements of a single array in ActionScript can contain data of different types, as noted in the sidebar "Arrays in Other Programming Languages". Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|