Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Cloning of array in ActionScript3

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 823
    Comment on it

    For the cloning of array in ActionScript there are no built in methods. Thus the cloning is done basically using two methods namely clone( ) or slice( ). For the shallow copy of the array these methods are used without any arguments. If in the original array the elements are the objects then during shallow copy only references of the objects are copied instead of the actual objects. If any changes are made in the object then it will be reflected in both the arrays. For example :

    var a:Array = new Array(1,2,3);
    var b:Array = a;
    


    In the above code it looks like we are assigning the value of a to b. The above code will not copy the elements of a to b because in ActionScript there are no copy constructor for array. So , if you really want to copy any array then you need to use any of the methods mentioned above.

    var a:Array = new Array (1,2,3);
    var b:Array=a.slice();
    


    If you want the clone the array that only has numbers and string then in that case the slice( ) should be used and if you have multi-dimensional array then in that case clone( ) should be used for the cloning.

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

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: