Follow Flex After Dark on Twitter

ActionScript Array

Arrays a common data structure in nearly any programming language, including ActionScript.

Characteristics of Arrays in ActionScript:

  • Arrays hold references to other objects, including null
  • Arrays are 0-based (like Java, unlike ColdFusion, for instance)
  • Arrays are unbounded meaning one will automatically grow as items are added

Examples

Below are several examples of array creation and population, each using slightly different techniques.

This example creates an empty array using the standard Array() constructor. Then several items are added to the array using the [] notation to explicitly set their index.

   // create an empty array then add elements
   var array1:Array = new Array();
   array1[0] = "A";
   array1[1] = "B";
   array1[2] = "C";
   trace( "Array has " + array1.length + " elements" ); // returns 3

This example creates an array by passing its elements in the constructor.

   // create an array with elements using the constructor
   var array2:Array = new Array( "A", "B", "C", "D", "E" );
   trace( "Array has " + array2.length + " elements" ); // returns 5

This example create an array using "short-hand" notation by surround the elements with [].

   // create an aray using "short-hand"
   var array3:Array = [ "A", "B", "C" ];
   trace( "Array has " + array3.length + " elements" ); // returns 3

This example creates an array again using the default constructor.

   // create an empty array then add scattered elements
   var array4:Array = new Array();
   array1[0] = "A";
   array1[1] = "B";
   array1[4] = "E";
   trace( "Array has " + array4.length + " elements" ); // returns 5

Even though only three elements were added to the array in the above example, the array is still considered to have five elements because of the scattered indexes.

Useful Methods

Below we show array functions in action:

   var array1:Array = new Array();
   
   // push() adds any number of passed elements to an array
   array1.push( "A", "B", "C" );
   trace( "Array has " + array1.length + " elements" ); // 3
   
   // concat() creates a new array with combined elements
   var array2:Array = array1.concat( "D", "E" );
   trace( "Array has " + array1.length + " elements" ); // 3
   trace( "Array has " + array2.length + " elements" ); // 5
   
   // join() converts all array elements to a string and 
   // separates them with the passed delimiter
   var joined:String = array2.join( "," );
   trace( "Joining of array2: " + joined ); // "A,B,C,D,E"
   
   // pop() removes and returns the last element of an array
   var removed:Object = array2.pop();
   trace( "Was removed: " + removed ); // "E"
   trace( "New length: " + array2.length ); // 4
   
   // reverse() reverses an array in place
   array2.reverse();
   trace( "New ordering: " + array2 ); // D,C,B,A

Array Looping

Looping over an array can done via "for" and "for each":

   // loop through each element of an array using "for each"
   var numbers:Array = new Array( 1, 2, 3, 4, 5 );
   var number:String;
   for each( number in numbers )
   {
      trace( number );
   }
   
   // loop through each element of an array using "for"
   var letters:Array = new Array( "A", "B", "C", "D", "E" );
   var index:int;
   for( index = 0; index < letters.length; index++ )
   {
      trace( "Element " + index + " is " + letters[index] );
   }

Array Sorting

Arrays can be sorted using the sort() function.

   // sort() sorts an array in place
   var letters:Array = new Array( "D", "A", "C", "B" );
   trace( "Old ordering: " + letters ); // D,A,C,B
   letters:Array.sort();
   trace( "New ordering: " + letters ); // A,B,C,D
   
   // sort() can also accept flags for sort options
   var numbers:Array = [ 22, 1, 13, 104, 200 ];
   trace( "Old ordering: " + numbers ); // 22,1,13,104,200
   numbers.sort( Array.NUMERIC );
   trace( "New ordering: " + numbers ); // 1,13,22,104,200

The numbers sorting example passes the Array.NUMERIC bit-flag to the sort() function. This ensures the numbers are properly sorted by numeric value (e.g. so 220 is sorted before 200).

ArrayCollections

While the Array is a useful and flexible data structure, in Flex it often makes sense to use a "Collection" object instead. Consider using the Array-powered ArrayCollection.

Related Links:

Related Docs
Recent Docs
  • ActionScript Array The ActionScript Array class and usage examples.
  • ActionScript vs Java ActionScript has many similarities to the Java programming language.
  • ActionScript XML ActionScript has powerful XML support via E4X.
  • ActionScript Get/Set Functions ActionScript Get/Set Functions are special function handlers for property accessors.
  • Flex Binding Flex Data Binding enables objects and their values to be bound together so that when a source changes a target automatically gets updated.
Random Docs