简体   繁体   English

如何在没有参考的情况下将元素列表添加到列表中?

[英]How add a List of Elements into a list without reference?

I cannot find an answer because it seems too much specific.我找不到答案,因为它看起来太具体了。 So here's my issue with C#.所以这是我的 C# 问题。 We can add another list to another one as a clone like this我们可以像这样将另一个列表添加到另一个列表作为克隆

list2 = new List<int>(list1);

What I want to know is how can I add a List into another one without any reference of the child?我想知道的是如何在没有任何孩子参考的情况下将列表添加到另一个列表中?

List<List<List<int>>> wireCoords = new List<List<List<int>>>();
List<int> coord = new List<int>();

for(int i = 0; i < inputSplits.Length; i++)
 {
      coord.Add(0);
      coord.Add(0);
      wireCoords[i].Add(coord);

 }

AS soon the wireCoords[0][0] list change, it also change inside wireCoords[1][0] How can I fix this?一旦 wireCoords[0][0] 列表发生变化,它也会在 wireCoords[1][0] 内部发生变化,我该如何解决这个问题?

Two things.两件事情。 You cannot access a List via [i] accessor unless it has content at that index.您不能通过 [i] 访问器访问列表,除非它在该索引处有内容。 Second, you can copy the values of a list by using List1.AddRange(List2).其次,您可以使用 List1.AddRange(List2) 复制列表的值。 After this, changing List2 will not change List1.在此之后,更改 List2 将不会更改 List1。

In your for loop, the number of items grow to inputSplits.Length * 2 for every index of wiredCoords.在您的 for 循环中,对于有线坐标的每个索引,项目数增长到 inputSplits.Length * 2。 To explain why this happens, lets take an example.为了解释为什么会发生这种情况,让我们举一个例子。

List<int> object1 = new List<int>();
object1.Add(1);
object1.Add(2);

List<int> object2 = object1;

object1.Add(3);
// at this time, object2 also has an element 3.

Console.WriteLine(string.Join(",", object2));

output:
1,2,3 (instead of 1,2 that you'd normally expect)

object1 never gets assigned the "value" of object2. object1 永远不会被分配 object2 的“值”。 object1 will get "reference" of the object2 and anywhere in code when you change values of object1, object2 will automatically get updated.当您更改对象 1 的值时,对象 1 将获得对象 2 和代码中任何位置的“引用”,对象 2 将自动更新。

Fix for that could be解决这个问题可能是

List<int> object2 = object1;
object1 = new List<int>(); // re-initialized
object1.Add(3);

// object1 has only 1 element
// object2 has 2 elements.

To resolve this, you create a new object or re-initialize the object to get a new reference and then use that for later assignments.要解决此问题,您可以创建一个新对象或重新初始化该对象以获取新引用,然后将其用于以后的分配。

Your code:您的代码:

List<List<List<int>>> wireCoords = new List<List<List<int>>>();
List<int> coord ;

for(int i = 0; i < inputSplits.Length; i++)
 {
      coord = new List<int>();
      coord.Add(0);
      coord.Add(0);
      wireCoords.Add(coord);

 }

Hope it helps.希望能帮助到你。

Adding to extensive explanation of the issue by @Jawad, here is how you can do that with LINQ in more concise and functional way:除了@Jawad 对这个问题的广泛解释之外,以下是您如何以更简洁和实用的方式使用 LINQ 做到这一点:

List<List<List<int>>> wireCoords = inputSplits
   .Select(_=> new List<int>(){0,0})
   .Select(coords=> new List<List<int>>(){coords})
   .ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM