View comments | RSS feed

Array()

Availability

Flash Player 6.

Usage

Array() : Array
Array(numElements:Number) : Array
Array( [element0:Object [, element1 , element2,...elementN ] ]) : Array

Parameters

element One or more elements to place in the array.

Returns

An array.

Description

Conversion function; creates a new, empty array or converts specified elements to an array. Using this function is similar to creating an array with the Array constructor (see Constructor for the Array class).

Example

Usage 1: The following example adds items to an array by using the array's index and then by using the Array class's push method:

var myArray:Array = Array();
myArray.push(12);
trace(myArray); //traces 12
myArray[4] = 7;
trace(myArray); //traces 12,undefined,undefined,undefined,7

Usage 2: The following example creates an array of length 4 but with no elements defined:

var myArray:Array = Array(4);
trace(myArray.length);  // traces 4
trace(myArray); // traces undefined,undefined,undefined,undefined

Usage 3: The following example creates an array with three defined elements:

var myArray:Array = Array(["firstElement", "secondElement", "thirdElement"]);
trace (myArray); // traces firstElement,secondElement,thirdElement

Note: Unlike the Array class constructor, the Array() function does not use the keyword new.

See also

Array class


Comments


ortho2005 said on Feb 7, 2005 at 9:30 AM :
To create multi-dimension array use this trick:

var arr:Array = new Array();
arr[0] = Array('0 first element', '0 second element');
arr[1] = Array('1 first element', '1second element');
arr[2] = Array('2 first element', '2 second element');

trace(arr[1][1]); // trace will be - 1 second element
No screen name said on Feb 28, 2005 at 1:20 PM :
concerning Usage 3

While it is true that this construction:

var myArray:Array = Array(["firstElement", "secondElement",
"thirdElement"]);

will yield this trace:

trace (myArray); // traces firstElement,secondElement,thirdElement

the statement at the beginning is not true:

Usage 3: The following example creates an array with three defined
elements

What it creates is an array with one element, "firstElement,
secondElement, thirdElement", and trace (myArray[0]) yields the full
string as the contents of index 0. Indices 1 and 2 remain undefined.

Removing the brackets in the original will accomplish what is intended,
i.e., an array with 3 elements:

var myArray:Array = Array("firstElement", "secondElement",
"thirdElement");
trace (myArray[0]); // traces firstElement
Francis Cheng said on Apr 14, 2005 at 10:13 PM :
Very good point, thanks for pointing that out, I'll fix the documentation.
fuGood said on Jun 14, 2005 at 5:22 AM :
Can someone enlight me of the differance betwwen an array, created by
"var myArray:Array = Array();"
and Array class instance, build by:
"var myArray:Array = new Array();"?

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/mx2004/main_7_2/00001190.html