我有以下代码: 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 ... 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... 我有以下代码: 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 ArrayListDim rNodeListNew As New ArrayListFor iCntr As Integer = 0 To tvRelations.Nodes.Count - 1 rNodeList.Add(tvRelations.Nodes(iCntr).Text)NextrNodeListNew = rNodeListrNodeList.RemoveAt(2)after I''ve filled rNodeList, it contains 8 elements.So does rNodeListNewBut when I remove the element 2 of rNodeList, it is also removed inrNodeListNew. Why ? Do they both point to the same memory location ?How can I prevent that in vb ?Thx
谢谢,那确实是克隆:) thanks, that''s clone indeed :)
更多推荐
arraylist副本
发布评论