ArrayCollection是flex中的数组集合类,它是很常用的,我们使用它时需要注意几个地方
1。事件监听, ArrayCollection可以为它注册一个集合改变的监听事件(CollectionEvent.COLLECTION_CHANGE),就是一旦ArrayCollection数组改变就会触发Event,不是所有情况的改变都会触发改变事件,如果集合当中的对象属性没有被绑定,那么你改变它的对象值也是不会触发事件的,在这种情况下你也许可能需要去将对象的属性进行绑定或者通过itemUpdated方法去管理对象值改变,除非集合的长度改变了,事件才会被触发
2。对象删除, ArrayCollection的对象删除方法removeAll(),有这样一种情况,当你在过滤集合数据的时候,它并不会删除所有数据,而是删除全部过滤的数据,不符合过滤条件的数据就没被删除,依然还在source中
3。过滤函数, ArrayCollection有个filterFunction过滤函数,就是可能集合中你只需要显示其中某几个对象,你将会需要根据对象条件筛选对象,那么你可能会用过滤函数,过滤函数会将不符合条件的对象过滤出来,但是ArrayCollection有个source属性是不会变的,它是个数组,所有源数据全在里面,尽管你去过滤,所有对象都会一直存在其中
4。排序, ArrayCollection还有一个sort属性是用来排序的,你可以为其指定排序字段
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);
}
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);
}
---------------------------------------------------------------------
-----------------------------------------------------------------------
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));
}