244
ActionScript classes
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
Содержание FLASH 8-ACTIONSCRIPT 2.0 LANGUAGE
Страница 1: ...ActionScript 2 0 Language Reference ...
Страница 1352: ...1352 ActionScript classes ...