Using Arrays in AS3 is a must its also important to have a good understand of best practices here are some best practices and tips & tricks that I use.
What is an Array?
An array is a collection of data, or in other words, a list of values.
Creating an Array
Index
Each item in an Array is assigned a numeric index, which uniquely identifies it and indicates the position of that item within the list.
Tip 1 - Off By One Error
ActionScript is a zero-based language, meaning that the first item in an Array has an index of 0 (zero).
Tip 2 - Be Specific
Always be specific when creating arrays
Tip 3 - Push is Good
Use array push to add items
Tip 4 - Adding Items
Use array unshift when adding items to the beginning and push to the end of the array
Tip 5 - Splice
Use array splice, shift and pop to remove items
//Remove one element from letters starting at index 1.
letters.splice(1, 1);
//Remove the first element and display its value.
trace(letters.shift( ));
//Remove the last element and display its value.
trace(letters.pop( ));
Tip 6 - Total Items
Use Array length to get the total items of an array.
Tip 7 - Loop
Looping through an array
Tip 8 - ArrayUtilities
Use the ArrayUtilities class for finding and matching items in an array
trace(ArrayUtilities.findMatchIndex(letters, "b"));
trace(ArrayUtilities.findMatchIndex(letters, "r"));
Tip 9 - CASAlib
Use CASAlib ActionScript library http://casalib.org/ which has addItemsAt and removeItem
var alphabet:Array = new Array("a", "d", "e");
var parts:Array = new Array("b", "c");
ArrayUtil.addItemsAt(alphabet, parts, 1);
trace(alphabet);
//removeItem
var numberArray:Array = new Array(1, 2, 3, 7, 7, 7, 4, 5);
trace("Removed " + ArrayUtil.removeItem(numberArray, 7) + " items.");
trace(numberArray);
Tip 10 - Concat
Use concat to copy an array
Tip 11 - Sort
Use the sort method for sorting elements of an array
words.sort( );
trace(words); //Displays: aardvark,jargon,relative,tricycle
Tip 12 - For Each
Thanks to Elad Elrom for reminding me about the forEach method

















223 Elad Elrom
Nov 06th, 2009 at 03:11 pm
Nice writeup, don’t forget “foreach” method:
forEach(callback:Function, thisObject:* = null):void
224 Almog http://www.almogdesign.net
Nov 06th, 2009 at 03:11 pm
Great point totally forgot about that thanks
406 peleg http://www.mogobe.com
Jan 04th, 2010 at 04:01 pm
you have a mistake at :
letters.push(”a”, “b”, “c”, “d”);
letters.unshift(”z”);
this 2 are not the same.
push add’s a value at the end of the array while
unshift add’s a value at the beginning of the array,
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Array.html#unshift()
410 peleg http://www.mogobe.com
Jan 05th, 2010 at 09:01 am
you wrote:
Tip 4 - Adding Items
Use array unshift when adding items to the end, and push to the being
var letters:Array = new Array( );
letters.push(”a”, “b”, “c”, “d”);
letters.unshift(”z”);
but this should be the opposite :
Use array unshift when adding items to the beggining, and push to the end of the Array.
have a great day
Peleg
411 Almog http://www.almogdesign.net
Jan 05th, 2010 at 06:01 pm
Thanks Peleg typing mistake fixed it