Array
----------------------------------------------------------------
1.
<mx:Script><![CDATA[
var myArray:Array = ["AK", "AL", "AR"];
]]></mx:Script>
2.
<mx:Array>
<mx:String>AK</mx:String>
<mx:String>AL</mx:String>
<mx:String>AR</mx:String>
</mx:Array>
3.
<mx:Script><![CDATA[
var expenses:Array = [
{Month: "January", Profit: 2000, Expenses: 1500, Amount: 450},
{Month: "February", Profit: 1000, Expenses: 200, Amount: 600},
{Month: "March", Profit: 1500, Expenses: 500, Amount: 300},
{Month: "April", Profit: 500, Expenses: 300, Amount: 500},
{Month: "May", Profit: 1000, Expenses: 450, Amount: 250},
{Month: "June", Profit: 2000, Expenses: 500, Amount: 700}
];
]]></mx:Script>
-----------------------------------------------------------------------
----------------------------------------------------------------------
// Use Array constructor.
var myArray:Array = new Array();
myArray.push("one");
myArray.push("two");
myArray.push("three");
trace (myArray); // output: one,two,three
// Use Array literal.
var myArray:Array = ["one", "two", "three"];
trace (myArray); // output: one,two,three
-----------------------------------------------------------------
----------------------------------------------------------------
var names:Array = new Array();
trace(names.length); // output: 0
var names:Array = new Array(3);
trace(names.length); // output: 3
trace(names[0]); // output: undefined
trace(names[1]); // output: undefined
trace(names[2]); // output: undefined
var names:Array = new Array("John", "Jane", "David");
trace(names.length); // output: 3
trace(names[0]); // output: John
trace(names[1]); // output: Jane
trace(names[2]); // output: David
-------------------------------------------------------------------
-----------------------------------------------------------------------
var planets:Array = new Array();
planets.push("Mars"); // array contents: Mars
planets.unshift("Mercury"); // array contents: Mercury,Mars
planets.splice(1, 0, "Venus", "Earth");
trace(planets); // array contents: Mercury,Venus,Earth,Mars
var oceans:Array = ["Victoria", "Pacific", "Aral", "Superior", "Indian", "Huron"];
oceans.splice(2, 2, "Arctic", "Atlantic"); // replaces Aral and Superior
oceans.pop(); // removes Huron
oceans.shift(); // removes Victoria
trace(oceans); // output: Pacific,Arctic,Atlantic,Indian
var lakes:Array = oceans.splice(2, 2, "Arctic", "Atlantic");
trace(lakes); // output: Aral,Superior
var oceans:Array = ["Arctic", "Pacific", "Victoria", "Indian", "Atlantic"];
delete oceans[2];
trace(oceans); // output: Arctic,Pacific,,Indian,Atlantic
trace(oceans[2]); // output: undefined
trace(oceans.length); // output: 5
var oceans:Array = ["Arctic", "Pacific", "Victoria", "Aral", "Superior"];
oceans.length = 2;
trace(oceans); // output: Arctic,Pacific
var oceans:Array = ["Arctic", "Atlantic", "Indian", "Pacific"];
oceans.reverse();
trace(oceans); // output: Pacific,Indian,Atlantic,Arctic
var poets:Array = ["Blake", "cummings", "Angelou", "Dante"];
poets.sort(); // default sort
trace(poets); // output: Angelou,Blake,Dante,cummings
poets.sort(Array.CASEINSENSITIVE);
trace(poets); // output: Angelou,Blake,cummings,Dante
poets.sort(Array.DESCENDING);
trace(poets); // output: Dante,cummings,Blake,Angelou
poets.sort(Array.DESCENDING | Array.CASEINSENSITIVE); // use two options
trace(poets); // output: Dante,cummings,Blake,Angelou
----------------------------------------------------------------------------
var names:Array = new Array("John Q. Smith", "Jane Doe", "Mike Jones");
function orderLastName(a, b):int {
var lastName:RegExp = /\b\S+$/;
var name1 = a.match(lastName);
var name2 = b.match(lastName);
if (name1 < name2) {
return -1;
} else if (name1 > name2) {
return 1;
} else {
return 0;
}
}
trace(names); // output: John Q. Smith,Jane Doe,Mike Jones
names.sort(orderLastName);
trace(names); // output: Jane Doe,Mike Jones,John Q. Smith
--------------------------------------------------------------------------
----------------------------------------------------------------------------
var poets:Array = new Array();
poets.push({name:"Angelou", born:"1928"});
poets.push({name:"Blake", born:"1757"});
poets.push({name:"cummings", born:"1894"});
poets.push({name:"Dante", born:"1265"});
poets.push({name:"Wang", born:"701"});
poets.sortOn("born", Array.NUMERIC);
for (var i:int = 0; i < poets.length; ++i) {
trace(poets[i].name, poets[i].born);
}
/* output:
Wang 701
Dante 1265
Blake 1757
cummings 1894
Angelou 1928
*/
var indices:Array;
indices = poets.sortOn("born", Array.NUMERIC | Array.RETURNINDEXEDARRAY);
for (var i:int = 0; i < indices.length; ++i) {
var index:int = indices[i];
trace(poets[index].name, poets[index].born);
}
/* output:
Wang 701
Dante 1265
Blake 1757
cummings 1894
Angelou 1928
*/
---------------------------------------------------------------------
-----------------------------------------------------------------------
var array1:Array = ["alpha", "beta"];
var array2:Array = array1.concat("gamma", "delta");
trace(array2); // output: alpha,beta,gamma,delta
var array3:Array = array1.concat(array2);
trace(array3); // output: alpha,beta,alpha,beta,gamma,delta
var array4:Array = array3.slice(2,5);
trace(array4); // output: alpha,beta,gamma
var rivers:Array = ["Nile", "Amazon", "Yangtze", "Mississippi"];
var riverCSV:String = rivers.toString();
trace(riverCSV); // output: Nile,Amazon,Yangtze,Mississippi
var riverPSV:String = rivers.join("+");
trace(riverPSV); // output: Nile+Amazon+Yangtze+Mississippi
var nested:Array = ["b","c","d"];
var letters:Array = ["a",nested,"e"];
var joined:String = letters.join("+");
trace(joined); // output: a+b,c,d+e
------------------------------------------------------------------
------------------------------------------------------------------
public dynamic class TypedArray extends Array {
private const dataType:Class;
public function TypedArray(...args) {
}
AS3 override function concat(...args):Array {
}
AS3 override function push(...args):uint {
}
AS3 override function splice(...args) {
}
AS3 override function unshift(...args):uint {
}
}
------------------------------------------------------------------
public dynamic class Array {
public function Array(...args) {
var n:uint = args.length
if (n == 1 && (args[0] is Number))
{
var dlen:Number = args[0];
var ulen:uint = dlen
if (ulen != dlen)
throw new RangeError("Array index is not a 32-bit unsigned integer ("+dlen+")")
length = ulen;
}
else
{
length = n;
for (var i:int=0; i < n; i++) {
this[i] = args[i]
}
}
}
}
-------------------------------------------------------------------------------------------------------
public dynamic class TypedArray extends Array {
private var dataType:Class;
public function TypedArray(typeParam:Class, ...args) {
dataType = typeParam;
var n:uint = args.length
if (n == 1 && (args[0] is Number))
{
var dlen:Number = args[0];
var ulen:uint = dlen
if (ulen != dlen)
throw new RangeError("Array index is not a 32-bit unsigned integer ("+dlen+")")
length = ulen;
}
else
{
for (var i:int=0; i < n; i++) {
// type check done in push()
this.push(args[i])
}
length = this.length;
}
}
}
-----------------------------------------------------------------------------------------------------
AS3 override function push(...args):uint {
for (var i:* in args) {
if (!(args[i] is dataType)) {
args.splice(i,1);
}
}
return (super.push.apply(this, args));
}
-----------------------------------------------------------------------------------------------------
AS3 override function concat(...args):Array {
var passArgs:TypedArray = new TypedArray(dataType);
for (var i:* in args) {
// type check done in push()
passArgs.push(args[i]);
}
return (super.concat.apply(this, passArgs));
}
-----------------------------------------------------------------------------------------------------
AS3 override function splice(...args):* {
if (args.length > 2) {
for (var i:int=2; i< args.length; i++) {
if (!(args[i] is dataType)) {
args.splice(i,1);
}
}
}
return (super.splice.apply(this, args));
}
-------------------------------------------------------------------------
AS3 override function unshift(...args):uint {
for (var i:* in args) {
if (!(args[i] is dataType)) {
args.splice(i,1);
}
}
return (super.unshift.apply(this, args));
}
}