如何在 ActionScript 3 中创建数组的浅表副本

发布于 2024-11-03 08:24:52 字数 123 浏览 1 评论 0原文

var a:Array = ["a","b","c"];

var b:Array;

/* insert code here to copy 'a' and assign it to 'b'*/
var a:Array = ["a","b","c"];

var b:Array;

/* insert code here to copy 'a' and assign it to 'b'*/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不必在意 2024-11-10 08:24:52

摘自 As3 参考指南:

Array类没有内置的
制作数组副本的方法。
您可以创建一个浅表副本
通过调用 concat() 来数组
或不带参数的 slice() 方法。
在浅拷贝中,如果原始
数组的元素是对象,
只有对对象的引用
复制而不是对象
他们自己。副本指向
与原始对象相同的对象。任何
对对象所做的更改是
反映在两个数组中。

如果您在连接和切片之间进行选择,连接将是最佳选择,因为连接在性能方面更快。

在此处阅读有关该主题的更多信息:http://help.adobe.com /en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7ee7.html

澄清:

    private function shallowCopy():void{
        var a:Array = ["h", "e", "l", "l", "o"];
        var b:Array = a.concat(); 

        trace("Shallow copy:");
        trace("Before delete: " + a);
        trace("Before delete: " + b);
        delete a[0];
        trace("After delete: " + a);
        trace("After delete: " + b);            
    }

Taken from the As3 reference guide:

The Array class has no built-in
method for making copies of arrays.
You can create a shallow copy of an
array by calling either the concat()
or slice() methods with no arguments.
In a shallow copy, if the original
array has elements that are objects,
only the references to the objects are
copied rather than the objects
themselves. The copy points to the
same objects as the original does. Any
changes made to the objects are
reflected in both arrays.

Concat would be the way to go if you choose between concat and slice since concat is faster in terms of performance.

Read more about the subject here: http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7ee7.html

To clarify:

    private function shallowCopy():void{
        var a:Array = ["h", "e", "l", "l", "o"];
        var b:Array = a.concat(); 

        trace("Shallow copy:");
        trace("Before delete: " + a);
        trace("Before delete: " + b);
        delete a[0];
        trace("After delete: " + a);
        trace("After delete: " + b);            
    }
吻风 2024-11-10 08:24:52

有问题的行:

var b:Array = a.concat();

The line in question:

var b:Array = a.concat();
一指流沙 2024-11-10 08:24:52

如果数组仅包含字符串或数字值,则足以制作“浅”副本,如 Adam 和 rzetterberg 所描述的那样。

如果数组包含其他数组或对象/类实例等,那么如果您需要内部的所有对象都是唯一的而不仅仅是引用,则应该进行深层复制。您可以通过以下方式实现:

var ba:ByteArray = new ByteArray(); 
ba.writeObject(a); // Copy the original array (a) into a ByteArray instance
ba.position = 0; // Put the cursor at the beginning of the ByteArray to read it
var b:Array = ba.readObject(); // Store a copy of the array in the destination array (b)
ba.clear(); // Free memory

这对于复制没有任何 concat 或 splice 方法的对象也很有用。

If the array contains just string or number values, it's enough to make a "shallow" copy, as described by Adam and rzetterberg.

If the array contains other arrays or objects/class instances, etc. then you should make a deep copy if you need all of the objects inside to be unique as well and not just references. You can achieve that with:

var ba:ByteArray = new ByteArray(); 
ba.writeObject(a); // Copy the original array (a) into a ByteArray instance
ba.position = 0; // Put the cursor at the beginning of the ByteArray to read it
var b:Array = ba.readObject(); // Store a copy of the array in the destination array (b)
ba.clear(); // Free memory

This is also useful for copying Objects, which don't have any concat or splice methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文