复制如何从一个arraylist包含到一个新的arraylist? [英] How do copy contains from one arraylist to a new arraylist ?

查看:142
本文介绍了复制如何从一个arraylist包含到一个新的arraylist?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





有人能告诉我如何将项目从一个arraylist复制到另一个arraylist吗?



我创建了一个arraylist并将值复制到new_arraylist。当我删除new_arraylist的东西时,它会从arraylist中删除。



谢谢





Fyi。我正在使用vb.net表单。

Hi,

Can someone tell me how to copy items from one arraylist to another arraylist ?

I created an arraylist and copied the value over to new_arraylist. When I delete something for new_arraylist it gets deleted from arraylist.

Thanks


Fyi. I am using vb.net forms.

推荐答案

new_arraylist = arraylist 的简单赋值仅复制引用对象,仍然只有一个 arraylist实例,其中两个引用

复制你需要创建一个新的arraylist实例的项目并在循环中逐个复制项目。

另请注意......如果arraylist中的项目具有引用语义(Class),那么只是复制对象的引用。

对于具有值语义的项目(例如,数字,字符串,结构),这两个列表最终没有重叠。
A simple assignment of new_arraylist = arraylist only copies the reference to the object, there still is only one arraylist instance with two references to it.
To copy the items you need to create a new arraylist instance and copy the items one-by-one in a loop.
Also note...if the items in the arraylist have "reference semantics" (Class) then that is just copying the references to the objects.
For items with "value semantics" (e.g., numbers, strings, Structures), the two lists will end up with no overlap.


这篇关于复制如何从一个arraylist包含到一个新的arraylist?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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