View comments | RSS feed

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, __proto__, prototype, __resolve


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, hasOwnProperty, isPropertyEnumerable, isPrototypeOf, registerClass, toString, unwatch, valueOf, watch



Version 8

Comments


No screen name said on Oct 1, 2005 at 12:28 PM :
var ar:Array = new Array(9999999999999);
trace(ar.length);

Why does this display 1316134911 ?
No screen name said on Dec 27, 2005 at 12:07 AM :
Well I will try to field this question the best I can... The max length for a Flash arary is 2,147,483,647... which is 2^31 - 1 so there are a max of 32 bits assigned to the length of a flash array. The last bit is the sign bit so if you put 2,147,483,648 and ask for the length of the array you will get -2,147,483,648 ... You don't really need an array that is over 32 bit do yoU!? =P well I'm not sure if you could keep track of the sign bit and figure out the length of your array that way. Even so there would still be a max lenght of your array but I think you should be able to get the full 32bits instead of the 31 one you get now.... Hope that helps! =)
Jackie_2004 said on Jan 11, 2006 at 9:24 AM :
hello guys!... i was wondering how could i make to store the position of the items from another arrays. Well i have this code that evaluates the genre of each song and i get an Array from here that tells according to the position in the array of the genre to which song it belongs, so it could have many repeated genres, and i would like to identify each genre and create another array, var or whatever to know exactly which items belongs to the genre eg of my code:

//if i have say 10 songs
//item 0 is for song 0, item 1 is for song 1 and so on...
var mygenres:Array=new Array("Rock", "Religious", "Punck", "Metal", "Rock", "Rock", "Other", "Metal", "Punck", "Other");
//and then i would like to get the results as array(or something similar would be very appreciated):
//the first item is for rock genre and should return and place the position where the genre was found and so on with the rest of the genres
var sortedGenres:Array=new Array([0, 4, 5], [1], [2, 8], [3, 7], [6,9]);

the problem is that is has become a little more complicated than i thought...due to i cannot have the position of the items inserted as i wish
thexgodfatherx69 said on Jan 12, 2006 at 5:50 PM :
The comment on Dec 27,2005 was left by me earlier... so I will try to answer this one as well...

Now... I'm not sure if this answer is right but I'll take a stab at it... I don't think you can have an Array of Arrays... which is it appears you are trying to do. I haven't had any experience with the whole music side of flash so I don't know how that works.

Maybe you should try sorting your music logically first all in one array. So if you 100 songs the first 10 are Rock the next 10 Punk... etc

Then if someone wanted to play a random punk song you could just generate a random number 0-9 and play that... I hope this helps maybe you could describe the situation a little bit more....
No screen name said on Jan 18, 2006 at 7:55 AM :
Hi Jackie_2004
This might help:
http://www.macromedia.com/go/tn_15144
DFS
No screen name said on Nov 28, 2006 at 1:59 AM :
I want to declare an Array that can be used in the following way;

myArray[i].description="lalala";
myArray[i].tag="this and that";
myArray[i].somethingElse="whatever";

so i guess what im asking is i want an array of an object, which is a set of strings ? i cant figure out how to construct it :(.

Any ideas ?
bespaly said on Apr 24, 2007 at 1:05 PM :
How can I store numbers in an array. Seems like the only examples I can find are for string values.
No screen name said on May 9, 2007 at 9:54 AM :
"I want to declare an Array that can be used in the following way..."
Try next:
var my_tmp_obj = new Object;
my_tmp_obj.description="lalala"; // or new String;
my_tmp_obj.tag="this and that";
my_tmp_obj.somethingElse="whatever";

var myArray:Array = new Array();
myArray.push(my_tmp_obj);
trace(myArray[0].somethingElse); // whatever

 

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

Current page: http://livedocs.adobe.com/flash/8/main/00001901.html