【问题标题】:Convert multidimensional array to list of single array将多维数组转换为单个数组的列表
【发布时间】:2015-12-30 09:02:13
【问题描述】:

我有一个多维数组,我需要将其转换为数组列表。不是一个数组,而是对于第一个维度的每次迭代,我需要一个包含第二个维度中的值的单独数组。

如何转换:

int[,] dummyArray = new int[,] { {1,2,3}, {4,5,6}};

到一个list<int[]> 持有两个数组,其值为{1,2,3}{4,5,6}

【问题讨论】:

  • 这个关键词是在线搜索的“扁平化”。
  • @BananaAcid,但 OP 不想要 List<int> 他想要 List<int[]>
  • @Grundy:对,但它应该有多“平坦”——我相信,示例代码可以调整。就像下面的 LINQ 示例链接一样,只需从中取出第一个“级别”。

标签: c#


【解决方案1】:

这是一个可重用的实现

public static class Utils
{
    public static List<T[]> To1DArrayList<T>(this T[,] source)
    {
        if (source == null) throw new ArgumentNullException("source");
        int rowCount = source.GetLength(0), colCount = source.GetLength(1);
        var list = new List<T[]>(rowCount);
        for (int row = 0; row < rowCount; row++)
        {
            var data = new T[colCount];
            for (int col = 0; col < data.Length; col++)
                data[col] = source[row, col];
            list.Add(data);
        }
        return list;
    }
}

和示例用法

var source = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
var result = source.To1DArrayList();

其他答案的一些问题。

M.kazem Akhgary: 如果我需要一个列表,我不明白为什么要先创建锯齿状数组并将其转换 list 而不是直接创建 list

Eser:我通常喜欢他优雅的 Linq 解决方案,但这绝对不是其中之一。如果这个想法是使用 Linq(尽管我坚信它不是为此而设计的),那么以下会更合适:

var source = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
var result = Enumerable.Range(0, source.GetLength(0))
    .Select(row => Enumerable.Range(0, source.GetLength(1))
    .Select(col => source[row, col]).ToArray())
    .ToList();

【讨论】:

    【解决方案2】:

    你可以像这样使用 for 循环:

            int[,] dummyArray = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
    
            int size1 = dummyArray.GetLength(1);
            int size0 = dummyArray.GetLength(0);
    
            List<int[]> list = new List<int[]>();
    
            for (int i = 0; i < size0; i++)
            {
                List<int> newList = new List<int>();
    
                for (int j = 0; j < size1; j++)
                {
                    newList.Add(dummyArray[i, j]);
                }
    
                list.Add(newList.ToArray());
            }
    

    【讨论】:

    • 为什么不直接使用数组,而是在列表中调用ToArray?你已经知道长度,那没有改变。
    【解决方案3】:

    你可以使用 Linq:

    int[,] dummyArray = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
    int count = 0;
    List<int[]> list = dummyArray.Cast<int>()
                        .GroupBy(x => count++ / dummyArray.GetLength(1))
                        .Select(g => g.ToArray())
                        .ToList();
    

    【讨论】:

      【解决方案4】:

      您可以将二维数组转换为jagged array,然后再转换为convert it to List

      int[,] arr = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
      
      int[][] jagged = new int[arr.GetLength(0)][];
      
      for (int i = 0; i < arr.GetLength(0); i++)
      {
          jagged[i] = new int[arr.GetLength(1)];
          for (int j = 0; j < arr.GetLength(1); j++)
          {
              jagged[i][j] = arr[i, j];
          }
      }
      
      List<int[]> list = jagged.ToList();
      

      【讨论】:

        猜你喜欢
        • 2020-03-24
        • 2011-10-10
        • 1970-01-01
        • 2013-05-17
        • 2015-12-24
        • 1970-01-01
        • 2012-04-04
        • 1970-01-01
        相关资源
        最近更新 更多