FLEX Collection Array ArrayCollection operation instance
<?xml version="1.0"encoding="Utf-8"?><!--Simple example to demonstrate the Halo DataGrid control. --><s:application xmlns:fx="http://ns.adobe.com/mxml/2009"xmlns:s="Library://ns.adobe.com/flex/spark"xmlns:mx="LIBRARY://NS.ADOBE.COM/FLEX/MX"> <fx:Script> <![cdata[Importmx.collections.ArrayCollection; //adding Objects Public functionInsertcollection ():void{ //Method 1 //a curly brace represents an object varSelecttype01:string ="Tokyo"; varSelecttype02:string ="Yokohama"; varOnearraycollection:arraycollection =NewArrayCollection ([{label:selecttype01, data:1}, {label:selecttype02, data:2}]); //Method 2 vartwoarraycollection:arraycollection; varArray:array =NewArray (); for(varI Number= 0; I < 10; i++) {array.unshift ({city:"B"+ I, DATA:I, size:1000 +i}); } twoarraycollection=NewArrayCollection (array);//The length is 10, which means there are 10 objects //Method 3 varThreearraycollection:arraycollection =Newarraycollection (); for(varJ: Number= 0; J < 5; J + +) {//Add 5 ObjectsThreearraycollection.additem ({name:"C"+ J, Sequence:j, OK:"Hello"+J}); } Trace (threearraycollection[2].ok);//Hello2 //Method 4, add the object at the specified indexThreearraycollection.additemat ({name:"K", Sequence:"A"Ok:"H"},2); Trace (threearraycollection[2].ok);//H //Method 5, add the object at the established indexThreearraycollection.setitemat ({name:"W", Sequence:"X"Ok:"Q"}, 5); Trace (threearraycollection[5].sequence);//X } //Delete object, arraycollection equivalent Array (array collection) Public functionDeleteCollection ():void{ varDelcollection:arraycollection =Newarraycollection (); for(varJ: Number= 0; J < 5; J + +) {//Add 5 ObjectsDelcollection.additem ({name:"DEL"+ J, Sequence:j, OK:"Hello"+J}); } for(varValue:stringinchdelcollection) {Trace (Delcollection[value].ok); } Delcollection.removeitemat (2);//Delete the third element for(varValue2:stringinchdelcollection) {Trace (Delcollection[value2].ok); } delcollection.removeall (); //Delete all for(varValue3:stringinchdelcollection) {Trace ("========="); Trace (Delcollection[value3].ok); } } //Querying Objects Public functionSelectcollection ():void{ vartwoarraycollection:arraycollection; varArray:array =NewArray (); for(varI Number= 0; I < 10; i++) {array.unshift ({city:"B"+ I, DATA:I, size:1000 +i}); } twoarraycollection=NewArrayCollection (array);//if the ArrayCollection method has parameters, it must be an array object as a parameter. if(Twoarraycollection.contains (array[1])){ varObjindex:int = Twoarraycollection.getitemindex (array[1]);//returns the index of the itemTrace (Objindex); } //Trace ("OK"); // } } ]]> </Fx:script> <mx:button id="Insertid"x=" the"Label="Add Object"click="Insertcollection ();" /> <mx:button id="Deleteid"x=" the"Label="Delete Object"click="DeleteCollection ();" /> <mx:button id="Seleteid"x=" the"Label="Query Object"click="Selectcollection ();" /> <fx:Declarations> <!--place non-visual elements (e.g., services, value objects) h Ere-</Fx:declarations> </S:application>
FLEX Collection Array ArrayCollection operation instance