繁体   English   中英

C#如何将字符串列表添加到列表

[英]C# How to add List of string to a list

我正在尝试将list of string添加到list 以下是我的代码

using System;
using System.Collections.Generic;

    public class Program
    {
        public static void Main()
        {
            List<string> customBindingRow = new List<string>();
            List<List<string>> customBindingData = new List<List<string>>();
            for (int i = 0; i < 10; i++)
            {
                customBindingRow.Clear();
                for (int j = 0; j < 2; j++)
                {
                    customBindingRow.Add("i=" + i.ToString() + "j=" + j.ToString());
                }

                customBindingData.Add(customBindingRow);
            }

            string text = "";
            foreach (List<string> dt in customBindingData)
            {
                text += string.Join(",", dt.ToArray()) + "\r\n";
            }

            Console.WriteLine(text);
        }
    }  

但是当我Console.WriteLine它仅显示最后添加的list

这是输出

i=9j=0,i=9j=1
i=9j=0,i=9j=1
i=9j=0,i=9j=1
i=9j=0,i=9j=1
i=9j=0,i=9j=1
i=9j=0,i=9j=1
i=9j=0,i=9j=1
i=9j=0,i=9j=1
i=9j=0,i=9j=1
i=9j=0,i=9j=1  

预期产量

i=0j=0,i=0j=1
i=1j=0,i=1j=1
i=2j=0,i=2j=1
i=3j=0,i=3j=1
i=4j=0,i=4j=1
i=5j=0,i=5j=1
i=6j=0,i=6j=1
i=7j=0,i=7j=1
i=8j=0,i=8j=1
i=9j=0,i=9j=1

实际上, customBindingRow是用于循环10次的同一对象。 您只需清除每个循环的内容即可。

您必须重新初始化customBindingRow ,而不是通过Clear初始化。

var customBindingData = new List<List<string>>();
for (int i = 0; i < 10; i++)
{
    var customBindingRow = new List<string>();
    for (int j = 0; j < 2; j++)
    {
        customBindingRow.Add("i=" + i.ToString() + "j=" + j.ToString());
    }

    customBindingData.Add(customBindingRow);
}

您添加在中创建的相同对象

List<string> customBindingRow = new List<string>();

通过清除并添加新字符串,可以对对象进行10次和10次修改。

只需用创建要插入集合的新对象来代替清除。

customBindingRow.Clear();

=>

customBindingRow = new List<string>();

您几乎接近预期的答案。 不要初始化customBindingRow,只需创建它,并将customBindingRow.Clear()行替换为customBindingRow = new List<string>; 因为.Clear()将从列表中删除所有项目。

 List<string> customBindingRow;
 List<List<string>> customBindingData = new List<List<string>>();

 for (int i = 0; i < 10; i++)
 {
      customBindingRow = new List<string>();
      for (int j = 0; j < 2; j++)
      {
           customBindingRow.Add("i=" + i.ToString() + "j=" + j.ToString());
      }

      customBindingData.Add(customBindingRow);
  }
  string text = "";
  foreach (List<string> dt in customBindingData)
  {
      text += string.Join(",", dt.ToArray()) + "\r\n";
  }

  Console.WriteLine(text);

希望这对您有所帮助。

在您的示例中,您修改了customBindingRow ,最后, customBindingData具有相同列表的十倍。

最后,对字符串进行分配不是有效的方法。 请改用StringBuilder类。

相反,您可以尝试这样:

List<string> customBindingRow;
List<List<string>> customBindingData = new List<List<string>>();
for (int i = 0; i < 10; i++)
{

    customBindingRow = new List<string>();
    for (int j = 0; j < 2; j++)
    {
        customBindingRow.Add("i=" + i.ToString() + "j=" + j.ToString());
    }

    customBindingData.Add(customBindingRow);
}

//Using stringbuilser will reduce the memory usage.
//Adding text to a string will create a new reference in memory, you should avoid this.
StringBuilder text = new StringBuilder();

foreach (List<string> dt in customBindingData)
{
    text.Append( string.Join(",", dt.ToArray()) + "\r\n");
}

Console.WriteLine(text.ToString());

暂无
暂无

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

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