Array


Object
    |
    +-Array

public dynamic class Array
extends Object

The Array class lets you access and manipulate indexed arrays. An indexed 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 indexed arrays are zero-based, which means that the first element in the array is [0], the second element is [1], and so on. To create an Array object, you use the constructor new Array(). To access the elements of an array, you use the array access ([]) operator.

You can store a wide variety of data types in an array element, including numbers, strings, objects, and even other arrays. You can create a multidimensional array by creating an indexed array and assigning to each of its elements a different indexed array. Such an array is considered multidimensional because it can be used to represent data in a table.

Array assignment is by reference rather than by value: when you assign one array variable to another array variable, both refer to the same array:

var oneArray:Array = new Array("a", "b", "c");
var twoArray:Array = oneArray; // Both array variables refer to the same array.
twoArray[0] = "z"; 
trace(oneArray); // Output: z,b,c.

The Array class should not be used to create associative arrays, which are different data structures that contain named elements instead of numbered elements. You should use the Object class to create associative arrays (also called hashes). Although ActionScript permits you to create associative arrays using the Array class, you can not use any of the Array class methods or properties. At its core, an associative array is an instance of the Object class, and each key-value pair is represented by a property and its value. Another reason to declare an associative array using the Object data type is that you can then use an object literal to populate your associative array (but only at the time you declare it). The following example creates an associative array using an object literal, accesses items using both the dot operator and the array access operator, and then adds a new key-value pair by creating a new property:

var myAssocArray:Object = {fname:"John", lname:"Public"};
trace(myAssocArray.fname); // Output: John
trace(myAssocArray["lname"]); // Output: Public
myAssocArray.initial = "Q";
trace(myAssocArray.initial); // Output: Q

Availability: ActionScript 1.0; Flash Player 5 - Became a native object in Flash Player 6, which improved performance significantly.

Example

In the following example, my_array contains four months of the year:

var my_array:Array = new Array();
my_array[0] = "January";
my_array[1] = "February";
my_array[2] = "March";
my_array[3] = "April";

Property summary

Modifiers

Property

Description

static

CASEINSENSITIVE:Number

In the sorting methods, this constant specifies case-insensitive sorting.

static

DESCENDING:Number

In the sorting methods, this constant specifies descending sort order.

 

length:Number

A non-negative integer specifying the number of elements in the array.

static

NUMERIC:Number

In the sorting methods, this constant specifies numeric (instead of character-string) sorting.

static

RETURNINDEXEDARRAY:Number

Specifies that a sort returns an indexed array as a result of calling the sort() or sortOn() method.

static

UNIQUESORT:Number

In the sorting methods, this constant specifies the unique sorting requirement.

Properties inherited from class Object

constructor (Object.constructor property), __proto__ (Object.__proto__ property), prototype (Object.prototype property), __resolve (Object.__resolve property)


Constructor summary

Signature

Description

Array([value:Object])

Lets you create an array.

Method summary

Modifiers

Signature

Description

 

concat([value:Object]) : Array

Concatenates the elements specified in the parameters with the elements in an array and creates a new array.

 

join([delimiter:String]) : String

Converts the elements in an array to strings, inserts the specified separator between the elements, concatenates them, and returns the resulting string.

 

pop() : Object

Removes the last element from an array and returns the value of that element.

 

push(value:Object) : Number

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

 

reverse() : Void

Reverses the array in place.

 

shift() : Object

Removes the first element from an array and returns that element.

 

slice([startIndex:Number], [endIndex:Number]) : Array

Returns a new array that consists of a range of elements from the original array, without modifying the original array.

 

sort([compareFunction:Object], [options:Number]) : Array

Sorts the elements in an array.

 

sortOn(fieldName:Object, [options:Object]) : Array

Sorts the elements in an array according to one or more fields in the array.

 

splice(startIndex:Number, [deleteCount:Number], [value:Object]) : Array

Adds elements to and removes elements from an array.

 

toString() : String

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

 

unshift(value:Object) : Number

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

Methods inherited from class Object

addProperty (Object.addProperty method), hasOwnProperty (Object.hasOwnProperty method), isPropertyEnumerable (Object.isPropertyEnumerable method), isPrototypeOf (Object.isPrototypeOf method), registerClass (Object.registerClass method), toString (Object.toString method), unwatch (Object.unwatch method), valueOf (Object.valueOf method), watch (Object.watch method)



Flash CS3