深拷贝ArrayList问题。 [英] Deep copy ArrayList problem.

查看:178
本文介绍了深拷贝ArrayList问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在ArrayList上执行深层复制。


我写了一个小样本应用来证明这可以做到:


ArrayList a = new ArrayList();

ArrayList b = new ArrayList();


a.Add(" Hello");

b =(ArrayList)a.Clone();


a [0] =" World";


这个出现工作正常。但是,当我在我的应用程序中尝试它时,

两个ArrayLists指向相同的内存。


我的ArrayList中的每个元素都是我编写的自定义类,所以我是

想知道这对Clone()的调用有什么影响吗?


在高层次上,我需要做的就是创建一个非常大的结构,一旦启动时就会产生
(ArrayList将是静态的),当每个请求来自客户端的
时,我会对结构进行深度复制并允许每个客户

他们自己的工作副本。这将减少我自己处理的大笔费用。


有人可以帮忙吗?


史蒂文


***通过开发人员指南 http:// www。 developersdex.com ***

I need to perform a Deep Copy on an ArrayList.

I wrote a small sample app to prove this could be done:

ArrayList a = new ArrayList();
ArrayList b = new ArrayList();

a.Add("Hello");
b = (ArrayList) a.Clone();

a[0] = "World";

This appears to work fine. However, when I try it in my application,
both ArrayLists point to the same memory.

Each element in my ArrayList is a custom class I have written, so I am
wondering would this have any impact on the call to Clone() ?

At a high level, what I need to do is create a very large structure once
on startup (the ArrayList will be static) and when each request comes in
from a client, I take a deep copy of the structure and allow each client
their own working copy. This would cut down a massive amoutn of
processing on my part.

Can anyone help?

Steven

*** Sent via Developersdex http://www.developersdex.com ***

推荐答案

尝试:


ArrayList a = new ArrayList ();

ArrayList b = new ArrayList();


a.Add(" Hello");

b = a;

Steven Blair写道:
try:

ArrayList a = new ArrayList();
ArrayList b = new ArrayList();

a.Add("Hello");
b = a;
Steven Blair wrote:

我需要在ArrayList上执行深层复制。


我写了一个小样本应用来证明这可以做到:


ArrayList a = new ArrayList();

ArrayList b = new ArrayList( );


a.Add(" Hello");

b =(ArrayList)a.Clone();


a [0] =" World";


这似乎工作正常。但是,当我在我的应用程序中尝试它时,

两个ArrayLists指向相同的内存。


我的ArrayList中的每个元素都是我编写的自定义类,所以我是

想知道这对Clone()的调用有什么影响吗?


在高层次上,我需要做的就是创建一个非常大的结构,一旦启动时就会产生
(ArrayList将是静态的),当每个请求来自客户端的
时,我会对结构进行深度复制并允许每个客户

他们自己的工作副本。这将减少我自己处理的大笔费用。


有人可以帮忙吗?


史蒂文


***通过开发人员指南 http:// www。 developersdex.com ***
I need to perform a Deep Copy on an ArrayList.

I wrote a small sample app to prove this could be done:

ArrayList a = new ArrayList();
ArrayList b = new ArrayList();

a.Add("Hello");
b = (ArrayList) a.Clone();

a[0] = "World";

This appears to work fine. However, when I try it in my application,
both ArrayLists point to the same memory.

Each element in my ArrayList is a custom class I have written, so I am
wondering would this have any impact on the call to Clone() ?

At a high level, what I need to do is create a very large structure once
on startup (the ArrayList will be static) and when each request comes in
from a client, I take a deep copy of the structure and allow each client
their own working copy. This would cut down a massive amoutn of
processing on my part.

Can anyone help?

Steven

*** Sent via Developersdex http://www.developersdex.com ***


Steven Blair写道:
Steven Blair wrote:

I需要在ArrayList上执行深层复制。


我写了一个小样本应用来证明这可以做到:


ArrayList a = new ArrayList();

ArrayList b = new ArrayList();


a.Add(" Hello");

b =(ArrayList)a.Clone();


a [0] =" World";


这似乎工作正常。但是,当我在我的应用程序中尝试它时,

两个ArrayLists都指向相同的内存。
I need to perform a Deep Copy on an ArrayList.

I wrote a small sample app to prove this could be done:

ArrayList a = new ArrayList();
ArrayList b = new ArrayList();

a.Add("Hello");
b = (ArrayList) a.Clone();

a[0] = "World";

This appears to work fine. However, when I try it in my application,
both ArrayLists point to the same memory.



我认为你可以使用ArrayList。 CopyTo和AddRange方法:


ArrayList a = new ArrayList();

ArrayList b = new ArrayList();


a.Add(" Hello");


string [a.Count] strings;

a.CopyTo(strings);


b.AddRange(字符串);


检查语法,但我认为这可行。

I think you can use the ArrayList.CopyTo along with AddRange method:

ArrayList a = new ArrayList();
ArrayList b = new ArrayList();

a.Add("Hello");

string[a.Count] strings;
a.CopyTo(strings);

b.AddRange(strings);

Check the syntax, but I think this may work.


Steven Blair< st ********** @ btinternet.comwrote:
Steven Blair <st**********@btinternet.comwrote:

我需要执行ArrayList上的深层复制。


我写了一个小样本应用来证明这可以做到:


ArrayList a = new ArrayList( );

ArrayList b = new ArrayList();


a.Add(" Hello");

b =( ArrayList)a.Clone();


a [0] =" World";


Th似乎工作正常。但是,当我在我的应用程序中尝试它时,

两个ArrayLists都指向相同的内存。
I need to perform a Deep Copy on an ArrayList.

I wrote a small sample app to prove this could be done:

ArrayList a = new ArrayList();
ArrayList b = new ArrayList();

a.Add("Hello");
b = (ArrayList) a.Clone();

a[0] = "World";

This appears to work fine. However, when I try it in my application,
both ArrayLists point to the same memory.



你能发一个简短但完整的程序来演示

的问题吗?


http://www.pobox.com/~skeet/csharp/ complete.html 了解详情

我的意思。

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.


我的ArrayList中的每个元素都是自定义类I写了,所以我想知道这会对克隆()的调用有什么影响吗?
Each element in my ArrayList is a custom class I have written, so I am
wondering would this have any impact on the call to Clone() ?



好​​吧,每个列表都会引用同一组对象开始,

尽管列表本身是独立的。如果你想要克隆对象

(正如你的主题所示),你需要在每个对象上调用Clone(),例如:


for(int i = 0; i< copiedList.Count; i ++)

{

copiedList [i] = copiedList [i] .Clone();

}


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件

Well, each list will refer to the same set of objects to start with,
although the lists themselves are independent. If you want the objects
themselves to be cloned (as your subject line suggests) you''ll need to
call Clone() on each of the objects, eg:

for (int i=0; i < copiedList.Count; i++)
{
copiedList[i] = copiedList[i].Clone();
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


这篇关于深拷贝ArrayList问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆