我怎么可以复制传输对象的ArrayCollection的动作?对象、动作、我怎么、ArrayCollection

2023-09-09 21:47:12 作者:左手边的位置,留给你

 公共函数创建():ArrayCollection的{

            VAR指数:=​​ 0;
            VAR数据:ArrayCollection的=新ArrayCollection的();
            VAR长度:INT = originalData.length;

            对于(指数;指数<长度;指数++){
                data.addItem(originalData [指数]);
            }

            返回的数据;
        }
 

originalData是我的数据从数据库中的原始状态。 数据originalData的副本用于被操纵的供应商名单的组成部分。 有一个按钮,我用它来拨打以上创建()函数,即 就意味着,我想要恢复数据的所有更改,并返回到 我的一切都在originalData。

但是,当我调试我的功能,originalData拥有所有的数据所做的更改。

当我使用

list.selectedItem.thing =新建字符串;

应该修改数据[指数] .thing,因为数据是List.dataProvider被我。但它改变originalData [指数] .thing也和这个集合不用于任何东西,除了打造自身的副本!

我不知道为什么会这样。我不知道如何短语这种行为作为谷歌查询。

为什么我复制图片到QQ聊天栏就变成传文件了

请,如果你不明白的问题,发表意见,所以我可以尝试,并使其更清晰。这已经消耗比它的功能更多的时间是值得的。

编辑:

我也试过,但它不工作:

 公共函数创建():ArrayCollection的{

            VAR指数:=​​ 0;
            VAR数据:ArrayCollection的=新ArrayCollection的();
            VAR长度:INT = originalData.length;

            对于(指数;指数<长度;指数++){
//初始化一个项目的对象。
                VAR的DataItem:项目=新项目();
                DataItem的= originalData [指数]的项目;
                data.addItem(DataItem的);
            }

            返回的数据;
        }
 

编辑2:

根据您的答案和一些研究,我想出了这个泛型函数的副本的自定义对象的arrayCollections。

 公共静态函数副本(objectClassName:字符串,对象类:类,ArrayCollection的:ArrayCollection的):ArrayCollection的{
            VAR指数:=​​ 0;
            VAR长度:INT = arrayCollection.length;
            VAR副本:ArrayCollection的=新ArrayCollection的();

            对于(指数;指数<长度;指数++){
                registerClassAlias​​(objectClassName,对象类);
                copy.addItemAt(ObjectUtil.copy(arrayCollection.getItemAt(指数))为对象类,索引);
            }
            返回副本;
        }
 

解决方案

由于汤姆说,这是因为AS3经过的参考。如果你不希望修改原始值,那么你应该再次为汤姆说,创建它们的副本。

幸运的是,AS3有一个工具来做到这一点 - ObjectUtils.copy 。试试这个code,而不是原来的:

 公共函数创建():ArrayCollection的{

            VAR指数:=​​ 0;
            VAR数据:ArrayCollection的=新ArrayCollection的();
            VAR长度:INT = originalData.length;

            对于(指数;指数<长度;指数++){
                data.addItem(mx.utils.ObjectUtil.copy(originalData [指数]));
            }

            返回的数据;
        }
 

请留意,副本()返回一个通用对象。如果您要访问它的任何一个类型安全方式的属性,你就必须将它转换为它的类型。

多一点的传递按引用的交易。比方说,我们有一个项目A,B和C在内存中飘来飘去。你把它们放到一个数组(originalData)。 originalData现在包含的引用的为A,B,和c。然后创建一个ArrayCollection和插入(再次)提及A,B,C。对象的a,b,和c没有什么被存储在存储阵列或一个ArrayCollection。所以,当你更新originalData [0]你要引用一个对象(一),并进行更新。

同样,当你更新ArrayCollection.getItemAt(0),你得到了相同的参考和更新这就是为什么你得到你要的行为的基本对象。进行复印并插入意味着你引用一个全新的对象。

public function create():ArrayCollection{

            var index:int = 0;
            var data:ArrayCollection = new ArrayCollection();
            var length:int = originalData.length;

            for(index; index < length; index++){
                data.addItem(originalData[index]);
            } 

            return data;
        }

originalData is the original state of my data from database. data is a copy of originalData used to be manipulated as the provider for my List component. There's a button I use to call the create() function above, that would mean, I want to revert all changes in data, and go back to everything I have in originalData.

But when I debug my function, originalData has all the changes made in data.

When I use

list.selectedItem.thing = "new string";

is supposed to modify data[index].thing, because data is my List.dataprovider. but it changes originalData[index].thing also and this collection wasn't used for anything, except for creating a copy of itself!

I don't know why this happens. I didn't know how to phrase this behaviour as a google query.

Please, if you don't understand the question, comment it so I can try and make it clearer. This has consumed more time than its functionality is worth.

EDIT:

I've also tried this, but it doesn't work:

public function create():ArrayCollection{

            var index:int = 0;
            var data:ArrayCollection = new ArrayCollection();
            var length:int = originalData.length;

            for(index; index < length; index++){
// initializing a Item object.
                var dataItem:Item = new Item();
                dataItem = originalData[index] as Item;
                data.addItem(dataItem);
            } 

            return data;
        }

EDIT 2:

Based on your answers and some research I came up with this generic function to copy arrayCollections made of custom objects.

public static function copy(objectClassName:String, objectClass:Class, arrayCollection:ArrayCollection):ArrayCollection{
            var index:int = 0;
            var length:int = arrayCollection.length;
            var copy:ArrayCollection = new ArrayCollection();

            for(index; index < length; index++){
                registerClassAlias(objectClassName,objectClass);
                copy.addItemAt(ObjectUtil.copy(arrayCollection.getItemAt(index)) as objectClass,index);
            }
            return copy;
        }

解决方案

As Tom says, this is because AS3 passes by reference. If you don't want to modify the original values then you should, again as Tom says, create copies of them.

Fortunately, AS3 has a utility to do this -- ObjectUtils.copy. Try this code instead of your original:

public function create():ArrayCollection{

            var index:int = 0;
            var data:ArrayCollection = new ArrayCollection();
            var length:int = originalData.length;

            for(index; index < length; index++){
                data.addItem(mx.utils.ObjectUtil.copy(originalData[index]));
            } 

            return data;
        }

Do take note that copy() returns a generic Object. If you want to access any of its properties in a type-safe manner you'll have to cast it to its type.

A bit more about the pass-by-reference deal. Let's say we have items a, b, and c floating around in memory. You put them into an array(originalData). originalData now contains references to a, b, and c. You then create an ArrayCollection and insert (again) references to a, b, c. Objects a, b, and c aren't what's being stored in either the array or the ArrayCollection. So when you update originalData[0] you're getting a reference to an object (a), and updating it.

Likewise when you update ArrayCollection.getItemAt(0) you're getting the same reference and updating the underlying object which is why you're getting the behavior you're getting. Making a copy and inserting it means you're referencing an entirely new object.