General Flex Tutorial – Part 5 – What is ActionScript (Array, ArrayCollection)?

This is the fifth in a series of posts making up a general tutorial on Adobe Flex. In this post I continue describing ActionScript, the programming language used to code the business logic in Flex applications. In this post I discuss the Array and ArrayCollection Flex classes.

Array and ArrayCollection – arrays allow you to store multiple values in one data structure. You can create simple indexed arrays or associative arrays, known in some programming languages as hashes.

 

Note: the sample code in this section presents creating Arrays and ArrayCollections in ActionScript, but you can create them in MXML as well. See the Flex Builder help system for examples of creating Arrays and ArrayCollections in MXML.

 

You can declare an indexed array and assign it values at the same time:

var clients:Array = ["Bob", "Jim", "Susan"];

 

Or you can declare the array in one statement and assign values later:

var clients:Array = new Array();
clients[0] = "Bob";
clients[1] = "Jim";
clients[2] = "Susan";

 

Associative arrays (which are actually of the Object class, not the Array class) use “keys” instead of indexes to reference individual elements in the array:

 

var foodPrices:Object = {meat:"5.00", fish:"7.00", juice:"1.00"};
trace(foodPrices ["meat"], foodPrices ["fish"], foodPrices["juice"]);

 

You can also do this:

var foodPrices:Object = new Object();
foodPrices["meat"] = "5.00";
foodPrices["fish"] = "7.00";
foodPrices["juice"] = "1.00";

 

You can also use object oriented “dot” notation to refer to associative array elements:

foodPrices.meat = "5.00";
foodPrices.fish = "7.00";
foodPrices.juice = "1.00";

 

trace(foodPrices.meat, foodPrices.fish, foodPrices.juice);

 

ArrayCollection is a Flex class that wraps the Array class in order to provide additional functionality, such as the following methods not found in the Array class:

 

addItem(item:Object):void

adds the item to the end of the ArrayCollection

 

addItemAt(item:Object, index:int):void

adds the item at the specified index

 

contains(item:Object):Boolean

returns true if the ArrayCollection contains the specified object

 

getItemAt(index:int, prefetch:int = 0):Object

gets the item at the specified index

 

getItemIndex(item:Object):int

returns the index of the item if it is in the ArrayCollection

 

refresh():Boolean

applies the sort and filter to the ArrayCollection

 

removeAll():void

remove all items from the ArrayCollection

 

Sometimes you need to check to see if two objects are the same object, often inside a function. You don’t need to iterate the ArrayCollection, just do this:

 

if(myObject = myAC.getItemAt(myAC.getItemIndex(myObject))){
       Trace(“Same object.”);
}else{
       Trace(“NOT same object.”);
}

 

You can create an instance of an ArrayCollection like this:

private var myAC:ArrayCollection = new ArrayCollection([
       "meat", "fish", "juice", "cheese"
]);

 

You can create an ArrayCollection of objects like this:

public var foodsAC:ArrayCollection;           
public function initData():void {
       foodsAC = new ArrayCollection(
              [{food:"meat", price:"5.00"},
              {food:"juice", price:"1.00"},
              {food:"cheese", price:"2.00"}
       ]);
}

Tags: , ,

Comments are closed.