【问题标题】:How to get the data inside a List<array>如何获取 List<array> 中的数据
【发布时间】:2015-08-20 06:56:59
【问题描述】:

示例...

List<array> thisListOfArray = new List<array>();
List<string> thisArrayA= new List<string>();
List<string> gMaintenanceB = new List<string>();
thisArrayA.Add("ItemA1");
thisArrayA.Add("ItemA2");
thisArrayA.Add("ItemA3");
thisArrayB.Add("ItemB1");
thisArrayB.Add("ItemB2");
thisArrayB.Add("ItemB3");
thisListOfArray.Add(thisArrayA.ToArray());
thisListOfArray.Add(thisArrayB.ToArray());

我想获取我在thisListOfArray 中输入的每个值。

【问题讨论】:

  • List&lt;string[]&gt;
  • C# 中,数组用type[] 表示。因此List&lt;array&gt; 需要变成List&lt;string[]&gt;

标签: c# arrays .net winforms


【解决方案1】:

您可以通过以下方式获得它。这是代码

List<string[]> thisListOfArray = new List<string[]>();
List<string> thisArrayA = new List<string>();
List<string> thisArrayB = new List<string>();
thisArrayA.Add("ItemA1");
thisArrayA.Add("ItemA2");
thisArrayA.Add("ItemA3");
thisArrayB.Add("ItemB1");
thisArrayB.Add("ItemB2");
thisArrayB.Add("ItemB3");
thisListOfArray.Add(thisArrayA.ToArray());
thisListOfArray.Add(thisArrayB.ToArray());

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

foreach (var strArray in thisListOfArray)
{
    foreach (var str in strArray)
    {
        lstNewstring.Add(str);
    }
}

MessageBox.Show(lstNewstring.Count.ToString());

【讨论】:

    【解决方案2】:

    像这样更改您的代码:

    List<List<string>> thisListOfArray = new List<List<string>>();
    List<string> thisArrayA = new List<string>();
    List<string> gMaintenanceB = new List<string>();
    thisArrayA.Add("ItemA1");
    thisArrayA.Add("ItemA2");
    thisArrayA.Add("ItemA3");
    gMaintenanceB.Add("ItemB1");
    gMaintenanceB.Add("ItemB2");
    gMaintenanceB.Add("ItemB3");
    thisListOfArray.Add(thisArrayA);
    thisListOfArray.Add(gMaintenanceB);
    
    foreach (var itm in thisListOfArray.SelectMany(item => item))
    {
        MessageBox.Show(itm);
    }
    

    【讨论】:

      【解决方案3】:

      如果你有:

      • List&lt;string[]&gt; listOfArray
      • string[] array

      使用listOfArray.Add(array); 将数组添加到列表中 不知道你会从哪里得到array

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-22
        • 1970-01-01
        • 1970-01-01
        • 2022-06-19
        • 1970-01-01
        • 2019-08-08
        相关资源
        最近更新 更多