473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Easy way to copy a List<List<T>>

List<List<T>a=param;
List<List<T>b=a;
If I change b, then a is get changed. I want another copy of a, that is
completely independent of a. I used double-nested for loop to copy each
element manually. Is there any more efficent way to do that? Something
like,
List<List<T>b=CreateClone(a);

Thanks.

Jul 28 '06 #1
4 33099
Sin Jeong-hun,

The easiest way to do this would be to use the ConvertAll method on the
instance itself. You can always convert the list to itself. =)

So, you can do:

// This assumes T is a valid type.
List<List<T>a = param;

// Make a copy.
List<List<T>b = a.ConvertAll(delegate(T input) { return input; });

Basically, you are passing the value through to a new list.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Sin Jeong-hun" <ty*******@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
List<List<T>a=param;
List<List<T>b=a;
If I change b, then a is get changed. I want another copy of a, that is
completely independent of a. I used double-nested for loop to copy each
element manually. Is there any more efficent way to do that? Something
like,
List<List<T>b=CreateClone(a);

Thanks.

Jul 28 '06 #2
Thank you all!

Nicholas Paldino [.NET/C# MVP] wrote:
Sin Jeong-hun,

The easiest way to do this would be to use the ConvertAll method on the
instance itself. You can always convert the list to itself. =)

So, you can do:

// This assumes T is a valid type.
List<List<T>a = param;

// Make a copy.
List<List<T>b = a.ConvertAll(delegate(T input) { return input; });

Basically, you are passing the value through to a new list.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Sin Jeong-hun" <ty*******@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
List<List<T>a=param;
List<List<T>b=a;
If I change b, then a is get changed. I want another copy of a, that is
completely independent of a. I used double-nested for loop to copy each
element manually. Is there any more efficent way to do that? Something
like,
List<List<T>b=CreateClone(a);

Thanks.
Jul 28 '06 #3
"Nicholas Paldino [.NET/C# MVP]" wrote:
// Make a copy.
List<List<T>b = a.ConvertAll(delegate(T input) { return input; });
This won't compile - you need to write "ConvertAll<List<T>>". More
importantly, this only clones the 'outer list' - you get a new list of
the same inner List<Telements. If you want a wholly independent copy
(so that changing one of the lists in the original tree doesn't affect
the copy) you need

List<List<T>DeepCopy =
List.ConvertAll<List<T>>(delegate(List<TBranch)
{
return Branch.ConvertAll<T>(delegate(T Leaf){return Leaf;});
});
--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn In print, in stores.
Jul 28 '06 #4
List<List<T>DeepCopy =
List.ConvertAll<List<T>>(delegate(List<TBranch)
{
return Branch.ConvertAll<T>(delegate(T Leaf){return Leaf;});
});
If T is a reference type, this method will still not be "deep" enough.
The leafs will just be refering to the same objects. To fix this I
guess you could make sure T inmplementes ICloneable and let Clone()
return a deap copy of T. Then the code should be:

List<List<T>DeepCopy =
List.ConvertAll<List<T>>(delegate(List<TBranch)
{
return Branch.ConvertAll<T>(delegate(T Leaf){return
Leaf.Clone();});
});

Aug 25 '06 #5

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

Similar topics

14
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for...
4
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>....
2
by: Brian Pelton | last post by:
I am not sure how to fix this problem I've stumbled into... I have a list<> of an interface type. I need to pass that list to a method that adds more objects to the list. But, eventually, I...
6
by: Jeff.Boeker | last post by:
I'm learning a lesson in how I need to be more specific :) In C++ I can resize a vector and it will allocate memory and it will call the default constructor if necessary (or I can supply an...
4
by: =?Utf-8?B?TGFycnlS?= | last post by:
I need some help with a multilevel sorting problem with the List<>. I have a List<ItemToSort( see below ) that needs to be sorted in the following manner: Sort by Level1Id ( ok that was the easy...
7
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list...
56
by: Zytan | last post by:
Obviously you can't just use a simple for loop, since you may skip over elements. You could modify the loop counter each time an element is deleted. But, the loop ending condition must be...
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
35
by: Lee Crabtree | last post by:
This seems inconsistent and more than a little bizarre. Array.Clear sets all elements of the array to their default values (0, null, whatever), whereas List<>.Clear removes all items from the...
9
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I've an optimization question for you all really quick. I have a stream that I am reading some bytes. At times, the stream can contain a small amount of bytes such as 50...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...
0
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...

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.