473,804 Members | 3,526 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ArrayList deep/shallow copy question

Hello,

Can someone explain me what is the differences between these 2 lines of
code :

MyArrayList2 = MyArrayList;
MyArrayList2 = MyArrayList.Clo ne();

MyArrayList/MyArrayList2 are aray lists of class objects implementing
the CompareTo() method, so the list is sortable.

What happens if i make this call : MyArrayList2.So rt() ?

Will MyArrayList be sorted too ?
Regards,
Cybertof.
Nov 15 '05 #1
3 4339
Cybertof,

Here are the differences:

MyArrayList2 = MyArrayList;

This will just copy the reference to MyArrayList into MyArrayList2. If
you add an element to MyArrayList2, then you will see that in MyArrayList,
because they both point to the same thing.

MyArrayList2 = MyArrayList.Clo ne();

This will create a shallow copy of the ArrayList. If the elements are a
reference, then those references are copied. If they are value types, then
that is copied to the new array list. Now, if they are all reference types,
then the two array lists are pointing to the same objects. However, if you
add a new item to MyArrayList2, then it will not be shown in MyArrayList,
because MyArrayList2 is a new object, not a reference to the same
MyArrayList.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Cybertof" <cy************ ****@gmx.net> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Hello,

Can someone explain me what is the differences between these 2 lines of
code :

MyArrayList2 = MyArrayList;
MyArrayList2 = MyArrayList.Clo ne();

MyArrayList/MyArrayList2 are aray lists of class objects implementing
the CompareTo() method, so the list is sortable.

What happens if i make this call : MyArrayList2.So rt() ?

Will MyArrayList be sorted too ?
Regards,
Cybertof.

Nov 15 '05 #2
Cybertof <cy************ ****@gmx.net> wrote:
Can someone explain me what is the differences between these 2 lines of
code :

MyArrayList2 = MyArrayList;
This makes the variable MyArrayList2 have the same value as MyArrayList
- in other words, they will both be references to the same object.
MyArrayList2 = MyArrayList.Clo ne();
This creates a *new* ArrayList which is a duplicate of the first list.
MyArrayList/MyArrayList2 are aray lists of class objects implementing
the CompareTo() method, so the list is sortable.

What happens if i make this call : MyArrayList2.So rt() ?

Will MyArrayList be sorted too ?


No. They are separate lists (you can remove things from one without it
affecting the others) - but the references *within* the lists haven't
been cloned, so if (say) the first element of MyArrayList is a
reference to a StringBuilder, and you append some text in that
StringBuilder, that append operation will be visible through the first
element of MyArrayList2 as they're both references to the same object.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
Nicholas & John :

Thanks for your both answers.
Regards,
Cybertof.
Nov 15 '05 #4

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

Similar topics

5
3291
by: Tony Johansson | last post by:
Hello! I'm reading in a book about C++ and that is something that sound strange. It says "Pointers have reference-assignment semantics similar to those in Java. For example, after the assignment Student* john = michael; both john and michael share the same object. This type of an assignment is different then value-assignmnet semantics used by class variables, as in
4
52312
by: fperfect13 | last post by:
Hi, I wanted to perform a deep copy of an array. Searching on google I ran into different opinions : C# Interview Questions (http://blogs.wwwcoder.com/tsvmadhav/archive/2005/04/08/2882.aspx) 10.What's the difference between the System.Array.CopyTo() and System.Array.Clone()?
6
4329
by: Desmond Cassidy | last post by:
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...
8
3141
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
2734
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
2001
by: pauldepstein | last post by:
I recently had a job interview question which I totally failed. (The question seemed excellent from an objective point of view, but having completely failed to do it, my subjective feelings are different.) It's hard for me to recall the question exactly. But basically, it involved a class construction with a shallow copy constructor. The question was basically "What is the bug in this code?" The intended answer was that shallow...
5
2475
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
15952
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
2633
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();
3
8754
by: raylopez99 | last post by:
The "C# Cookbook" (O'Reilly / Jay Hilyard), section 3.26, is on deep cloning versus shallow cloning. The scanned pages of this book are found here: http://www.sendspace.com/file/mjyocg (Word format, 3 pp) My question, coming from a C++ background where deep copying is done, is why in C# you would do either deep or shallow copying as suggested by O'Reilly (using the "ICloneable" inhereited interface), at least for the .NET framework. ...
0
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9588
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
9161
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
7625
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
6857
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
5527
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...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3828
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.