【问题标题】:How do I add a string array within a string array array in a string list?如何在字符串列表的字符串数组中添加字符串数组?
【发布时间】:2019-02-10 10:21:45
【问题描述】:

我有一个string[,] 和一定数量的string[],它们保持不变,我想从中挑选一些并将它们放在list<string[]> 中(我使用一个列表使其可扩展)。但是list<string[]> 不会接受来自string[,] 的任何string[]

string[,] testArray =
{
    {"testValue1", "testValue1.1"}
    {"testValue2", "testValue2.1"}
    {"testValue3", "testValue3.1"}
}

List<string[]> testList = new List<string[]>();

testList.Add(testArray[1]) //error: wrong number of indicaters inside[]
testList.Add(testArray[1,1]) //error: cannot convert from string to string[]

【问题讨论】:

  • testList.Add(new [] {testArray[1,1]}) 等等...
  • string[,] 不是数组数组,它​​是二维数组。也许你想要字符串[][]?那里的 testArray 是宽度为 2 和高度为 3 的字符串矩阵,并且没有简单的方法来迭代单行/列,您必须为此执行自己的 for 循环。 (或者可能是扩展方法)
  • “字符串列表”是 List&lt;string&gt;,而不是 List&lt;string[]&gt;

标签: c# arrays string


【解决方案1】:

你所拥有的是一个二维数组,而不是 jagged array(数组数组)。

您不能直接引用二维数组的整行(与锯齿状数组不同)。 简单的解决方案是使用锯齿状数组string[][],但如果您明确不想这样做,您可以通过自己循环二维数组的行并将其缓冲到另一个数组中来实现相同的目的

string[,] testArray =
{
    {"testValue1", "testValue1.1"},
    {"testValue2", "testValue2.1"},
    {"testValue3", "testValue3.1"}
};

List<string[]> testList = new List<string[]>();
string[] arrayRow = new string[testArray.GetLength(1)]; // zero index dimension to get length of

int i = 0; // row to copy
for (int j = 0; j < testArray.GetLength(1); j++) { // loop over row
    arrayRow[j] = testArray[i, j];
}
testList.Add(arrayRow); // {"testValue1", "testValue1.1"} added to list

【讨论】:

    【解决方案2】:

    二维数组可以使用交错数组或for循环:

    锯齿状数组:

    new string[] {"testValue1", "testValue1.1"},
    new string[] {"testValue2", "testValue2.1"},
    new string[] {"testValue3", "testValue3.1"}
    };
    
    List<string[]> testList = new List<string[]>();
    
    testList.Add(testArray[1]); //second row of jagged array
    

    二维数组:

    string[,] testArray =
    {
     {"testValue1", "testValue1.1"},
     {"testValue2", "testValue2.1"},
     {"testValue3", "testValue3.1"}
    };
    
    List<string[]> testList = new List<string[]>();
    string[] secnd_rw=new string[testArray.GetLength(1)] ;
    for (int i = 0; i < testArray.GetLength(1); i++)
    {
        secnd_rw[i] = testArray[1,i];
    }
    testList.Add(secnd_rw); //second row of 2d array
    

    【讨论】:

      【解决方案3】:

      我有一个字符串[,],其中包含一定数量的字符串[]

      这实际上是不正确的,因为您使用的是多维数组而不是锯齿状数组。如果您使用的是[,],则不能像使用[][] 一样直接取出整行。

      锯齿状

      string[][] testArray =
      {
          new [] {"testValue1", "testValue1.1"},
          new [] {"testValue2", "testValue2.1"},
          new [] {"testValue3", "testValue3.1"}
      };
      
      Console.WriteLine(string.Join(", ", testArray[0])); //testValue1, testValue1.1
      

      二维数组

      string[,] testArray =
      {
          {"testValue1", "testValue1.1"},
          {"testValue2", "testValue2.1"},
          {"testValue3", "testValue3.1"}
      };
      
      Console.WriteLine(string.Join(", ", testArray.GetColumn(0))); //testValue1, testValue1.1
      
      public static class ArrayExt
      {
          public static IEnumerable<string> GetColumn(this string[,] array, int column)
          {
              return Enumerable.Repeat(column, array.GetLength(1))
                  .Zip(Enumerable.Range(0, array.GetLength(1)), (x,y) => new { X = x, Y = y })
                  .Select(i => array[i.X, i.Y]);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-10-02
        • 1970-01-01
        • 2013-12-28
        • 2013-04-11
        • 2015-03-09
        • 1970-01-01
        • 2011-08-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多