簡體   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