arraylist副本 [英] arraylist copy

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

问题描述




我有以下代码:


Dim rNodeList As New ArrayList

Dim rNodeListNew作为新的ArrayList


对于iCntr As Integer = 0到tvRelations.Nodes.Count - 1

rNodeList.Add(tvRelations.Nodes(iCntr).Text)

下一页


rNodeListNew = rNodeList

rNodeList.RemoveAt(2)


在我填充了rNodeList之后,它包含了8个元素。

rNodeListNew是这样的


但是当我删除rNodeList的元素2时,它也会被删除在

rNodeListNew。为什么?它们都指向相同的内存位置吗?

如何在vb中阻止它?


Thx

Hi,

I have the following code :

Dim rNodeList As New ArrayList
Dim rNodeListNew As New ArrayList

For iCntr As Integer = 0 To tvRelations.Nodes.Count - 1
rNodeList.Add(tvRelations.Nodes(iCntr).Text)
Next

rNodeListNew = rNodeList
rNodeList.RemoveAt(2)

after I''ve filled rNodeList, it contains 8 elements.
So does rNodeListNew

But when I remove the element 2 of rNodeList, it is also removed in
rNodeListNew. Why ? Do they both point to the same memory location ?
How can I prevent that in vb ?

Thx

推荐答案

2005年4月8日

将行rNodeListNew = rNodeList切换为:


rNodeList.CopyTo(rNodeListNew)


这应该将对象复制到新的arraylist中。这样,

arraylists不会指向相同的对象。如果你使用.Clone方法

那么两个arraylists仍将指向相同的对象。 HTH


Joseph MCAD


" Sam" < SA ************** @ voila.fr>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...
April 8, 2005

Switch the line rNodeListNew = rNodeList to:

rNodeList.CopyTo(rNodeListNew)

This should COPY the objects into the new arraylist. This way the
arraylists do not point to the same objects. If you use the .Clone method
then both arraylists will still point to the same objects. HTH

Joseph MCAD

"Sam" <sa**************@voila.fr> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...


我有以下代码:

Dim rNodeList As New ArrayList
Dim rNodeListNew As New ArrayList

对于iCntr As Integer = 0到tvRelations.Nodes.Count - 1
rNodeList.Add(tvRelations.Nodes(iCntr).Text)
下一页

rNodeListNew = rNodeList
rNodeList.RemoveAt(2)

在我填充rNodeList后,它包含8个元素。
rNodeListNew

但当我删除元素2对于rNodeList,它也在
rNodeListNew中删除。为什么?它们是否都指向相同的内存位置?
如何在vb中阻止它?

Thx
Hi,

I have the following code :

Dim rNodeList As New ArrayList
Dim rNodeListNew As New ArrayList

For iCntr As Integer = 0 To tvRelations.Nodes.Count - 1
rNodeList.Add(tvRelations.Nodes(iCntr).Text)
Next

rNodeListNew = rNodeList
rNodeList.RemoveAt(2)

after I''ve filled rNodeList, it contains 8 elements.
So does rNodeListNew

But when I remove the element 2 of rNodeList, it is also removed in
rNodeListNew. Why ? Do they both point to the same memory location ?
How can I prevent that in vb ?

Thx



尝试使用ArrayList的.Clone()方法。


Marcie


2005年4月8日09:24:51 -0700,"山姆" < SA ************** @ voila.fr>写道:
Try the .Clone() method of the ArrayList.

Marcie

On 8 Apr 2005 09:24:51 -0700, "Sam" <sa**************@voila.fr> wrote:


我有以下代码:

Dim rNodeList As New ArrayList
Dim rNodeListNew As新的ArrayList

对于iCntr As Integer = 0到tvRelations.Nodes.Count - 1
rNodeList.Add(tvRelations.Nodes(iCntr).Text)
下一页

rNodeListNew = rNodeList
rNodeList.RemoveAt(2)

在我填充rNodeList后,它包含8个元素。
rNodeListNew
<但是当我删除rNodeList的元素2时,它也会在
rNodeListNew中删除。为什么?它们是否都指向相同的内存位置?
如何在vb中阻止它?

Thx
Hi,

I have the following code :

Dim rNodeList As New ArrayList
Dim rNodeListNew As New ArrayList

For iCntr As Integer = 0 To tvRelations.Nodes.Count - 1
rNodeList.Add(tvRelations.Nodes(iCntr).Text)
Next

rNodeListNew = rNodeList
rNodeList.RemoveAt(2)

after I''ve filled rNodeList, it contains 8 elements.
So does rNodeListNew

But when I remove the element 2 of rNodeList, it is also removed in
rNodeListNew. Why ? Do they both point to the same memory location ?
How can I prevent that in vb ?

Thx





谢谢,那确实是克隆:)

thanks, that''s clone indeed :)


这篇关于arraylist副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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