这次我遇到了一个 C# 编程问题:
我有一个列表列表:
List<List<string>> List1
以及其中的字符串列表:
List<string> List2
并且,在循环中,我想将 List2 放入 List1 中,但是随后,我想更改 List2 中的值,但我不想更改此值List2 中的值被插入到 List1 中。
这是我想要做的伪代码示例(因为我想目前没有人得到我想要的东西):
List1 <=> ['one', 'two', 'three']
List2[1] <=> List1
List1.clear
List1 <=> ['four', 'five', 'six']
List2[2] <=> List1
但是 List1 在 List2[1] 中也发生了变化 - 我不希望这样!现在看起来像:
List2[1] <=> ['four', 'five', 'six']
List2[2] <=> ['four', 'five', 'six']
但我希望它会是:
List2[1] <=> ['one', 'two', 'three']
List2[2] <=> ['four', 'five', 'six']
有什么意义?我希望这些 List1 保持在我将它们插入 List2 时的状态。我希望有人能理解这一点并帮助我。
我知道我在 List2 中插入某种 List1 指针,但如何使其正确呢? (右 -> 我希望它的工作方式^^)
this time I've got an C# programming issue:
I've got a list of lists:
List<List<string>> List1
And that list of strings in it:
List<string> List2
And, in loop, I want to put List2 in List1, but then, I want to change values in List2, but I don't want to change this values in the List2 which was inserted in List1.
Here is the example with pseudo-code of what I want to do (because I suppose nobody gets what I want at the moment):
List1 <=> ['one', 'two', 'three']
List2[1] <=> List1
List1.clear
List1 <=> ['four', 'five', 'six']
List2[2] <=> List1
but List1 changed in List2[1] too - I don't want that! It looks like now:
List2[1] <=> ['four', 'five', 'six']
List2[2] <=> ['four', 'five', 'six']
But I want that it will be:
List2[1] <=> ['one', 'two', 'three']
List2[2] <=> ['four', 'five', 'six']
What's the point? I want that these List1s stay in that state, what there were when I was inserting them to the List2. I hope someone will understand this and will help me.
I know that I'm inserting some kind of List1 pointners in List2, but how make it right? (right -> the way I want it to work^^)
发布评论
评论(6)
您想要将
List1
的副本放入List2
中:否则,
List1
、List2[1]
和List2[2]
引用相同的列表,因此通过更改其中一个列表,您也会更改其他列表。You want to put a copy of
List1
inList2
:Otherwise,
List1
,List2[1]
andList2[2]
reference the same list, so by changing one, you're also changing the others.您需要为外部列表中的每个项目创建一个新列表。请记住,您将在外部列表中放置对内部列表的引用,而不是副本。
you need to create a new list for each item in the outer list. Remember, you are placing a reference to the inner list in the outer list, not a copy.
我们明白。 List1 正在“更改”的原因是因为 List1 是指向内存中存储其项目的位置的指针。因此,当您分配 List2[1] = List1 时,您只是创建了另一个名为 List2[1] 的指针,指向内存中的同一位置。
为了完成你所说的,你必须在每次通过循环时创建一个新列表。例如:
在每次循环中,List one 将是一个新对象。最后,列表二将包含以下值:
[["one1", "two1", " Three1"], ["one2", "two2", " Three2"], ... ]
您可以生成您可以以任何方式值,但我只是展示了一个基于循环计数器使它们动态化的示例。
We understand. The reason List1 is "changing" is because List1 is a pointer to a place in memory where its items are stored. So when you assign List2[1] = List1, you're just creating another pointer called List2[1] to the same place in memory.
To accomplish what you're talking about, you'll have to create a new list each time you pass through the loop. So for example:
In each pass through this loop, List one will be a new object. At the end, List two will have these values in it:
[["one1", "two1", "three1"], ["one2", "two2", "three2"], ... ]
You can generate the values any way you wish, but I was just showing an example of making them dynamic based on the loop counter.
这是我知道的一个答案。当您想要存储列表的副本而不是对原始列表的引用时,您必须执行深复制类型的操作。不幸的是,深复制不是框架的一部分。
通过博客上的评论者“karl”( http://openmymind.net/ ) http://weblogs.asp.net/jeff/archive/2005/11/21/431125.aspx 带有我自己的编辑以添加到示例中...您可以序列化和反序列化列表,您将获得列表的真实副本:
Here's one answer I know. You have to do a deep-copy type of operation when you want to store a copy of a list rather than a reference to the original. Deep copy is unfortunately not part of the framework.
Via the commenter "karl" ( http://openmymind.net/ ) on the blog at http://weblogs.asp.net/jeff/archive/2005/11/21/431125.aspx with my own edits to add to the example... you can serialize and deserialize the list, and you get a true copy of the list:
尝试翻译此页面以了解:http://leonelfraga.com/neomatrixtech/?p=412
请注意代码差异以及暗框中打印的内容。
Try to translate this page to understand: http://leonelfraga.com/neomatrixtech/?p=412
Note the code diferences and what is being printed in the dark boxes.
为了达到您想要的结果,您需要存储您想要修改的列表的副本。您可以通过创建新列表并手动添加原始列表中的元素,或者使用扩展方法
ToList(...)
来完成此操作。这将创建原始列表的副本,该副本将被存储,以便您可以修改原始列表而不影响副本。To achieve your desired result, you need to store a copy of the list you wish to modify. You can do this either by creating a new list and adding the elements from the original in manually, or by using the extension method
ToList(...)
. This will create a copy of your original list, which will be stored, so you can then modify the original list without affecting the copy.