Flex中ArrayCollection的复制(克隆)

转载地址:http://blog.csdn.net/accp_fangjian/article/details/2423591

在Flex的开发过程中,常常会遇到复制ArrayCollection的情况。以前只是从网上找个方法拿来就用,也没有具体的搞清楚所以然来。今天有时间,研究了一下,总结如下:

        Flex中的复制同java一样,也分为深复制和浅复制。(如果您对java的深复制和浅复制理解不深的话,可以参考我前一篇文章:JAVA中浅复制与深复制

        下面总结一下ArrayCollection的浅复制,共有4种方法:

        1、var newArray:ListCollectionView=new ListCollectionView(ListCollectionView(oldArray).list);

        2、var newArray:ArrayCollection=new ArrayCollection(oldArray.source);

        3、var newArray:ArrayCollection=new ArrayCollection(oldArray.source.slice());

        4、var newArray:ArrayCollection=new ArrayCollection(oldArray.toArray());

        以上方法中,第一种方法由于不是创建的独立的ArrayCollection对象,所以不推荐使用。第二、三、四种方法相似,可以参考使用。

        有的时候,我们也会用到深复制,方法如下:

        var newArray:ArrayCollection=ArrayCollection(this.clone(oldArray));

        this.clone()方法定义如下:

         function clone(source:Object):*
         {
                var myBA:ByteArray = new ByteArray();
                myBA.writeObject(source);
                myBA.position = 0;
                return(myBA.readObject());
         }

下面是我测试以上程序的Flex程序代码,大家可以参考:

<? xml version="1.0" encoding="utf-8" ?>
< mx:Application  xmlns:mx ="http://www.adobe.com/2006/mxml"  creationComplete ="initApp()"  layout ="absolute" >
    
< mx:Script >
    
<![CDATA[
        import mx.collections.ArrayCollection;
        import mx.collections.ListCollectionView;
        import mx.controls.Alert;
        import flash.utils.ByteArray;
        
        
        public function initApp():void
        {            
        }
        
        
        //开始测试:
        //浅复制1
        private function test1():void
        {
            //初始化需要的对象
            var obj1:Object=new Object();//定义对象obj1
            var obj11:Object=new Object();//定义对象obj1的引用对象obj11
            obj1.name="obj1.name";
            obj1.sub=obj11;//obj1的sub属性引用obj11
            obj11.name="obj11.name";            
            //实例化ArrayCollection对象,并将obj1添加进该对象
            var oldArray:ArrayCollection=new ArrayCollection();
            oldArray.addItem(obj1);
            
            //浅复制ArrayCollection
            var newArray:ListCollectionView=new ListCollectionView(ListCollectionView(oldArray).list);
            
            //修改obj11.name的值
            obj11.name="obj11.name is changed!";
            
            //输出复制前后的两个对象所引用的obj11对象的name属性值,如一致为浅复制,不一致为深复制
            Alert.show(oldArray.getItemAt(0).sub.name+"---"+newArray.getItemAt(0).sub.name);
        }
        //浅复制2
        private function test2():void
        {
            //初始化需要的对象
            var obj1:Object=new Object();//定义对象obj1
            var obj11:Object=new Object();//定义对象obj1的引用对象obj11
            obj1.name="obj1.name";
            obj1.sub=obj11;//obj1的sub属性引用obj11
            obj11.name="obj11.name";            
            //实例化ArrayCollection对象,并将obj1添加进该对象
            var oldArray:ArrayCollection=new ArrayCollection();
            oldArray.addItem(obj1);
            
            //浅复制ArrayCollection
            var newArray:ArrayCollection=new ArrayCollection(oldArray.source);
            
            //修改obj11.name的值
            obj11.name="obj11.name is changed!";
            
            //输出复制前后的两个对象所引用的obj11对象的name属性值,如一致为浅复制,不一致为深复制
            Alert.show(oldArray.getItemAt(0).sub.name+"---"+newArray.getItemAt(0).sub.name);
        }
        
        //浅复制3
        private function test3():void
        {
            //初始化需要的对象
            var obj1:Object=new Object();//定义对象obj1
            var obj11:Object=new Object();//定义对象obj1的引用对象obj11
            obj1.name="obj1.name";
            obj1.sub=obj11;//obj1的sub属性引用obj11
            obj11.name="obj11.name";            
            //实例化ArrayCollection对象,并将obj1添加进该对象
            var oldArray:ArrayCollection=new ArrayCollection();
            oldArray.addItem(obj1);
            
            //浅复制ArrayCollection
            var newArray:ArrayCollection=new ArrayCollection(oldArray.source.slice());
            
            //修改obj11.name的值
            obj11.name="obj11.name is changed!";
            
            //输出复制前后的两个对象所引用的obj11对象的name属性值,如一致为浅复制,不一致为深复制
            Alert.show(oldArray.getItemAt(0).sub.name+"---"+newArray.getItemAt(0).sub.name);
        }
        
        //浅复制4
        private function test4():void
        {
            //初始化需要的对象
            var obj1:Object=new Object();//定义对象obj1
            var obj11:Object=new Object();//定义对象obj1的引用对象obj11
            obj1.name="obj1.name";
            obj1.sub=obj11;//obj1的sub属性引用obj11
            obj11.name="obj11.name";            
            //实例化ArrayCollection对象,并将obj1添加进该对象
            var oldArray:ArrayCollection=new ArrayCollection();
            oldArray.addItem(obj1);
            
            //浅复制ArrayCollection
            var newArray:ArrayCollection=new ArrayCollection(oldArray.toArray());
            
            //修改obj11.name的值
            obj11.name="obj11.name is changed!";
            
            //输出复制前后的两个对象所引用的obj11对象的name属性值,如一致为浅复制,不一致为深复制
            Alert.show(oldArray.getItemAt(0).sub.name+"---"+newArray.getItemAt(0).sub.name);
        }
        
        //深复制
        private function test5():void
        {
            //初始化需要的对象
            var obj1:Object=new Object();//定义对象obj1
            var obj11:Object=new Object();//定义对象obj1的引用对象obj11
            obj1.name="obj1.name";
            obj1.sub=obj11;//obj1的sub属性引用obj11
            obj11.name="obj11.name";            
            //实例化ArrayCollection对象,并将obj1添加进该对象
            var oldArray:ArrayCollection=new ArrayCollection();
            oldArray.addItem(obj1);
            
            //浅复制ArrayCollection
            var newArray:ArrayCollection=ArrayCollection(this.clone(oldArray));
            
            //修改obj11.name的值
            obj11.name="obj11.name is changed!";
            
            //输出复制前后的两个对象所引用的obj11对象的name属性值,如一致为浅复制,不一致为深复制
            Alert.show(oldArray.getItemAt(0).sub.name+"---"+newArray.getItemAt(0).sub.name);
        }
        
        //深复制调用的方法
        function clone(source:Object):*
        {
            var myBA:ByteArray = new ByteArray();
            myBA.writeObject(source);
            myBA.position = 0;
            return(myBA.readObject());
        }
    
]]>
    
</ mx:Script >
    
< mx:Button  label ="第一种浅复制方法"  click ="test1()"  x ="61"  y ="36" />
    
< mx:Button  label ="第二种浅复制方法"  click ="test2()"  y ="57"  x ="61"  width ="114" />
    
< mx:Button  label ="第三种浅复制方法"  click ="test3()"  y ="78"  x ="61"  width ="114" />
    
< mx:Button  label ="第四种浅复制方法"  click ="test4()"  y ="98"  x ="61"  width ="114" />
    
    
< mx:Button  label ="深复制方法"  click ="test5()"  y ="147"  x ="61" />
</ mx:Application >

你可能感兴趣的:(arrayCollection)