Array class

Availability

Flash Player 5 (became a native object in Flash Player 6, which improved performance significantly).

Description

The Array class lets you access and manipulate arrays. An array is an object whose properties are identified by a number representing their position in the array. This number is referred to as the index. All arrays are zero-based, which means that the first element in the array is [0], the second element is [1], and so on. In the following example, my_array contains the months of the year.

my_array[0] = "January"
my_array[1] = "February"
my_array[2] = "March"
my_array[3] = "April"

To create an Array object, use the constructor new Array() or the array access operator ([]). To access the elements of an array, use the array access operator ([]).

Method summary for the Array class

Method

Description

Array.concat()

Concatenates the parameters and returns them as a new array.

Array.join()

Joins all elements of an array into a string.

Array.pop()

Removes the last element of an array and returns its value.

Array.push()

Adds one or more elements to the end of an array and returns the array's new length.

Array.reverse()

Reverses the direction of an array.

Array.shift()

Removes the first element from an array and returns its value.

Array.slice()

Extracts a section of an array and returns it as a new array.

Array.sort()

Sorts an array in place.

Array.sortOn()

Sorts an array based on a field in the array.

Array.splice()

Adds and removes elements from an array.

Array.toString()

Returns a string value representing the elements in the Array object.

Array.unshift()

Adds one or more elements to the beginning of an array and returns the array's new length.

Property summary for the Array class

Property

Description

Array.length

A nonzero-based integer specifying the number of elements in the array.

Constructor for the Array class

Availability

Flash Player 5.

Usage

new Array()
new Array(length)
new Array(element0, element1, element2,...elementN)

Parameters

length An integer specifying the number of elements in the array. In the case of noncontiguous elements, the length parameter specifies the index number of the last element in the array plus 1.

element0...elementN A list of two or more arbitrary values. The values can be numbers, strings, objects, or other arrays. The first element in an array always has an index or position of 0.

Returns

Nothing.

Description

Constructor; lets you create an array. You can use the constructor to create different types of arrays: an empty array, an array with a specific length but whose elements have no values, or an array whose elements have specific values.

Usage 1: If you don't specify any parameters, an array with a length of 0 is created.

Usage 2: If you specify only a length, an array is created with length number of elements with no values.

Usage 3: If you use the element parameters to specify values, an array is created with specific values.

Example

Usage 1: The following example creates a new Array object with an initial length of 0.

my_array = new Array();
trace(my_array.length); // returns 0

Usage 2: The following example creates a new Array object with an initial length of 4.

my_array = new Array(4);
trace(my_array.length); // returns 4

Usage 3: The following example creates the new Array object go_gos_array, with an initial length of 5.

go_gos_array = new Array("Belinda", "Gina", "Kathy", "Charlotte", "Jane");
trace(my_array.length); // returns 5
trace(go_gos_array.join(", ")); // displays elements

The initial elements of the go_gos array are identified as follows:

go_gos_array[0] = "Belinda";
go_gos_array[1] = "Gina";
go_gos_array[2] = "Kathy";
go_gos_array[3] = "Charlotte";
go_gos_array[4] = "Jane";

The following code adds a sixth element to the go_gos_array array and changes the second element:

go_gos_array[5] = "Donna";
go_gos_array[1] = "Nina"
trace(go_gos_array.join(" + "));

See also

Array.length, [] (array access)