Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Updated 20-Dec-13 12:53pm
v3

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900