List<List<T>a=p aram;
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=C reateClone(a);
Thanks. 4 33135
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(de legate(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.co m
"Sin Jeong-hun" <ty*******@gmai l.comwrote in message
news:11******** **************@ m79g2000cwm.goo glegroups.com.. .
List<List<T>a=p aram;
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=C reateClone(a);
Thanks.
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(de legate(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.co m
"Sin Jeong-hun" <ty*******@gmai l.comwrote in message
news:11******** **************@ m79g2000cwm.goo glegroups.com.. .
List<List<T>a=p aram;
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=C reateClone(a);
Thanks.
"Nicholas Paldino [.NET/C# MVP]" wrote:
// Make a copy.
List<List<T>b = a.ConvertAll(de legate(T input) { return input; });
This won't compile - you need to write "ConvertAll<Lis t<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>Dee pCopy =
List.ConvertAll <List<T>>(deleg ate(List<TBranc h)
{
return Branch.ConvertA ll<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.
List<List<T>Dee pCopy =
List.ConvertAll <List<T>>(deleg ate(List<TBranc h)
{
return Branch.ConvertA ll<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>Dee pCopy =
List.ConvertAll <List<T>>(deleg ate(List<TBranc h)
{
return Branch.ConvertA ll<T>(delegate( T Leaf){return
Leaf.Clone();}) ;
}); This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 storing vertices of an arbitrary n-ary
tree where a vertex contain pointers to its parent / children. These parent
/ child vertices need to stay put if we've got pointers to them somewhere!
Am I correct in my assertion?
|
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>. However, I
need to cast that list to a List<BaseClass> and it is not working. The
code is below. I get the following exception:
"Unable to cast object of type 'System.Collections.Generic.List`1' to
type 'System.Collections.Generic.List`1'."
...
|
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 need to get back to the original list<> object.
---
In other words; let's say I start with an List<> of 3 objects. I call a
|
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 instance
for the copy constructor).
For example:
C++
vector<classvClass;
|
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 part)
Within the unique Level1Id's, sort by Level2Id
Within the unique level2Id's sort by name, then description.
I looked at doing this with some foreach and list.FindAll() and copying the
list into a new list, but this had some code smells....
| |
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 without
itterating through the entire collection and building up a list?
2. is there a common base class, collection or interface that can contain
either/both of these collection types and then how do you convert or cast
from the base to either a...
|
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 checked on each iteration,
since the Count changes as you delete elements. I would think it is
guaranteed to be computed each time, and not cached.
So, is this the best way?
|
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 of their elements?
Why can't I change the element itself?
class Program
{
private struct MyStruct
|
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 list.
That part makes a reasonable amount of sense, as you can't actually take
items away from an Array. However, there doesn't seem to be a way to
perform the same operation in one fell swoop on a List<>.
For example:
|
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 or so or it can contain as much 10000000 bytes.
In reality, I do not know the maximum number of bytes.
In my function, I am going to read() the byte stream using a buffer. Now,
is it better to read it into a buffer and dump the buffer into a...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |