473,785 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ArrayList Copy (Deep)

Hi,
I'm sure this has been asked several times before but I'll risk it ;-)

If I wish to save an Arraylist to another Arraylist and work on te
original without affecting the contents of the new or saved Arraylist, how
is it done. It always seems to create a shallow copy i.e. by Reference.
I even tried passing the original Arraylist to a function by Value and
return a copy....after an arraylist.clear on the original it still clears
the saved Arraylist.

Thus the code I would like would be similar to the following

larrlstArrayLis tSave = larrlstArrayLis tOriginal
larrlstArrayLis tOriginal.Clear
BUT the larrlstArrayLis tSave should still have the collection in there.

I looked at .Clone and .CopyTo but they don't seem to cut !!!

It's annoying the hell out of me !!!

Any ideas ???

Cheers,

Desmond
Nov 21 '05 #1
6 4323

"Desmond Cassidy" <De************ *@T-Online.de> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hi,
I'm sure this has been asked several times before but I'll risk it ;-)

If I wish to save an Arraylist to another Arraylist and work on te
original without affecting the contents of the new or saved Arraylist, how
is it done. It always seems to create a shallow copy i.e. by Reference.
I even tried passing the original Arraylist to a function by Value and
return a copy....after an arraylist.clear on the original it still clears
the saved Arraylist.

Thus the code I would like would be similar to the following

larrlstArrayLis tSave = larrlstArrayLis tOriginal
larrlstArrayLis tOriginal.Clear
BUT the larrlstArrayLis tSave should still have the collection in
there.

I looked at .Clone and .CopyTo but they don't seem to cut !!!

It's annoying the hell out of me !!!

Any ideas ???

Cheers,

Desmond

Clone will do it if the elements in your ArrayList are primitives, but if
you're storing reference types in the ArrayList, it looks like you'll have
to do the necessary work of cloning each of the actual objects in your own
"DeepCopy" method.
I'm a bit confused by your example, however, as Clear() simply sets the
ArrayList Count property to zero, thus the references established in the
second ArrayList still point to the objects not deleted by Clear().
There may be a better solution/technique that I've missed.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
Nov 21 '05 #2
OK...I found something written about this a few weeks ago...and it seems to
have caused some confusion.

CopyTo and Clone seem to be the buzz words.

In my case I 'should' use Clone as this creates a DEEP copy whereas
CopyTo will NOT work with an Arraylist.
CopyTo will copy an Arraylist to an Array Type and NOT an Arraylist.

Thus, and I would like the gurus in you to confirm this.....

A Shallow copy is
larrlstArrayLis tSave = larrlstArrayLis tOriginal
A Deep Copy is
larrlstArrayLis tSave =
DirectCast(larr lstArrayListOri ginal.Clone(),A rrayList)

Cheers,

Desmond.
"Desmond Cassidy" <De************ *@T-Online.de> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hi,
I'm sure this has been asked several times before but I'll risk it ;-)

If I wish to save an Arraylist to another Arraylist and work on te
original without affecting the contents of the new or saved Arraylist, how
is it done. It always seems to create a shallow copy i.e. by Reference.
I even tried passing the original Arraylist to a function by Value and
return a copy....after an arraylist.clear on the original it still clears
the saved Arraylist.

Thus the code I would like would be similar to the following

larrlstArrayLis tSave = larrlstArrayLis tOriginal
larrlstArrayLis tOriginal.Clear
BUT the larrlstArrayLis tSave should still have the collection in
there.

I looked at .Clone and .CopyTo but they don't seem to cut !!!

It's annoying the hell out of me !!!

Any ideas ???

Cheers,

Desmond

Nov 21 '05 #3
Hi Peter,
Thanks for replying.....

I am storing plain old strings and it seems to work OK...so I should
STILL watch it if the type is a strong reference type ??

Cheers,

Desmond.

"Desmond Cassidy" <De************ *@T-Online.de> wrote in message
news:OF******** ******@TK2MSFTN GP14.phx.gbl...
OK...I found something written about this a few weeks ago...and it seems
to have caused some confusion.

CopyTo and Clone seem to be the buzz words.

In my case I 'should' use Clone as this creates a DEEP copy whereas
CopyTo will NOT work with an Arraylist.
CopyTo will copy an Arraylist to an Array Type and NOT an Arraylist.

Thus, and I would like the gurus in you to confirm this.....

A Shallow copy is
larrlstArrayLis tSave = larrlstArrayLis tOriginal
A Deep Copy is
larrlstArrayLis tSave =
DirectCast(larr lstArrayListOri ginal.Clone(),A rrayList)

Cheers,

Desmond.
"Desmond Cassidy" <De************ *@T-Online.de> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hi,
I'm sure this has been asked several times before but I'll risk it ;-)

If I wish to save an Arraylist to another Arraylist and work on te
original without affecting the contents of the new or saved Arraylist,
how is it done. It always seems to create a shallow copy i.e. by
Reference.
I even tried passing the original Arraylist to a function by Value and
return a copy....after an arraylist.clear on the original it still clears
the saved Arraylist.

Thus the code I would like would be similar to the following

larrlstArrayLis tSave = larrlstArrayLis tOriginal
larrlstArrayLis tOriginal.Clear
BUT the larrlstArrayLis tSave should still have the collection in
there.

I looked at .Clone and .CopyTo but they don't seem to cut !!!

It's annoying the hell out of me !!!

Any ideas ???

Cheers,

Desmond


Nov 21 '05 #4
Desmond.

You have to make a deepCopy as Peter already wrote. I never found a standard
answer how to make a deepCopy on MSDN.

Problem is that it is probably impossible because of the fact that an
arraylist holds references to objects, wich can be everything. In fact they
can all be different, not very usefull, however it is possible.

Do you have any idea how to copy the content of this arraylist without
knowing what it is. I don't for me it looks impossible (I as well don't know
how to use it when I don't keep track what every reference per index holds).
\\\
Dim ar As New ArrayList
ar.Add(New Integer)
ar.Add(New Double)
Dim str As String
ar.Add(str)
Dim arl2 As New ArrayList
Dim arl3 As New ArrayList
arl2.Add(arl3)
ar.Add(arl2)
///
Before you say this is crazy, I try only to show you what it can be.

However serializing and deserializing can be a good workaround for this
problem.

\\\
Private Function SerializeArrayl ist(ByVal _
arraylst As ArrayList) As String
Dim bf As New
Runtime.Seriali zation.Formatte rs.Binary.Binar yFormatter
Dim mem As New IO.MemoryStream
bf.Serialize(me m, arraylst)
Return Convert.ToBase6 4String(mem.ToA rray())
End Function
Private Function DeserializeArra ylist(ByVal _
arraystring As String) As ArrayList
Dim bf As New
Runtime.Seriali zation.Formatte rs.Binary.Binar yFormatter
Dim mem As New
IO.MemoryStream (Convert.FromBa se64String(arra ystring))
Return DirectCast(bf.D eserialize(mem) , ArrayList)
End Function
///

I hope this helps,

Cor

Nov 21 '05 #5
Many thanks Cor.....


"Cor Ligthert" <no************ @planet.nl> wrote in message
news:up******** ******@TK2MSFTN GP10.phx.gbl...
Desmond.

You have to make a deepCopy as Peter already wrote. I never found a
standard answer how to make a deepCopy on MSDN.

Problem is that it is probably impossible because of the fact that an
arraylist holds references to objects, wich can be everything. In fact
they can all be different, not very usefull, however it is possible.

Do you have any idea how to copy the content of this arraylist without
knowing what it is. I don't for me it looks impossible (I as well don't
know how to use it when I don't keep track what every reference per index
holds).
\\\
Dim ar As New ArrayList
ar.Add(New Integer)
ar.Add(New Double)
Dim str As String
ar.Add(str)
Dim arl2 As New ArrayList
Dim arl3 As New ArrayList
arl2.Add(arl3)
ar.Add(arl2)
///
Before you say this is crazy, I try only to show you what it can be.

However serializing and deserializing can be a good workaround for this
problem.

\\\
Private Function SerializeArrayl ist(ByVal _
arraylst As ArrayList) As String
Dim bf As New
Runtime.Seriali zation.Formatte rs.Binary.Binar yFormatter
Dim mem As New IO.MemoryStream
bf.Serialize(me m, arraylst)
Return Convert.ToBase6 4String(mem.ToA rray())
End Function
Private Function DeserializeArra ylist(ByVal _
arraystring As String) As ArrayList
Dim bf As New
Runtime.Seriali zation.Formatte rs.Binary.Binar yFormatter
Dim mem As New
IO.MemoryStream (Convert.FromBa se64String(arra ystring))
Return DirectCast(bf.D eserialize(mem) , ArrayList)
End Function
///

I hope this helps,

Cor

Nov 21 '05 #6
On 2005-04-24, Desmond Cassidy <De************ *@T-Online.de> wrote:
Hi Peter,
Thanks for replying.....

I am storing plain old strings and it seems to work OK...so I should
STILL watch it if the type is a strong reference type ??
No, clone does not make a deep copy.

OTOH, if all you're using is strings, then it doesn't matter. Strings
are immutable. The reason you want a deep copy is so that changes you
make to the objects in arraylist1 don't affect the objects in
arraylist2.

With strings, though, you can't change them anyway. So it really
doesn't matter if you have a shallow or deep copy of the array list.
"Desmond Cassidy" <De************ *@T-Online.de> wrote in message
news:OF******** ******@TK2MSFTN GP14.phx.gbl...
OK...I found something written about this a few weeks ago...and it seems
to have caused some confusion.

CopyTo and Clone seem to be the buzz words.

In my case I 'should' use Clone as this creates a DEEP copy whereas
CopyTo will NOT work with an Arraylist.
CopyTo will copy an Arraylist to an Array Type and NOT an Arraylist.

Thus, and I would like the gurus in you to confirm this.....

A Shallow copy is
larrlstArrayLis tSave = larrlstArrayLis tOriginal
No, that simply creates another variable pointing to the same
arraylist. You haven't really copied anything.
A Deep Copy is
larrlstArrayLis tSave =
DirectCast(larr lstArrayListOri ginal.Clone(),A rrayList)


That's a shallow copy. Both array lists now reference the same objects,
but in two separate lists.

Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1251
by: Marc Lefebvre | last post by:
there is an easy way to make a deep copy of an arraylist ? suppose I have an arraylist of COptions implements ICloneable I have need to overload the Clone() methode of a derived ArrayList (OptionsArrayList) And Overload the Clone() methode of my COptions There is an otherway to do deep copy ?
1
3394
by: Marc Lefebvre | last post by:
there is an easy way to make a deep copy of an arraylist ? suppose I have an arraylist of COptions implements ICloneable I have need to overload the Clone() methode of a derived ArrayList (OptionsArrayList) And Overload the Clone() methode of my COptions There is an otherway to do deep copy ?
1
2557
by: Marc Lefebvre | last post by:
there is an easy way to make a deep copy of an arraylist ? suppose I have an arraylist of COptions implements ICloneable I have need to overload the Clone() methode of a derived ArrayList (OptionsArrayList) And Overload the Clone() methode of my COptions There is an otherway to do deep copy ?
15
8337
by: Sam | last post by:
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
8
3139
by: Esmail Bonakdarian | last post by:
Hello, I am relatively new to C# but like it. I have run into a problem to which I haven't been able to find a solution despite looking. I have an ArrayList that contains several RichTextBoxes (via Add()) I am trying to create a second ArrayList with this information, but I want to make a DEEP copy instead of a shallow one, so that
2
2732
by: Lubomir | last post by:
Hi, I would like to ask if the constructor ArrayList(ICollection c) performs the deep or shallow copy of "c" collection. If it perfoms just shallow copy, is there any method for doing a deep one? Thanks, Lubomir
5
2471
by: tshad | last post by:
How do you easily make a copy of an arraylist? If you do: arrayList2 = arrayList1 You get a pointer so that if you clear arrayList2 (arrayList2.Clear) - arrayList1 is also cleared. I want to create a copy of the arrayList, which I can do looping through the
22
15948
by: Steven Blair | last post by:
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();
3
2631
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: code:------------------------------------------------------------------------------ class Program { static public ArrayList MyArrayList = new ArrayList();
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.