【问题标题】:C# split 1D array into 2D array for every Nth valueC# 将 1D 数组拆分为每 N 个值的 2D 数组
【发布时间】:2018-09-21 12:58:17
【问题描述】:

我有一个一维整数数组:

int[] array = { 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31, 32,33, 34,40,41,42,43, 44};

我想将此一维数组划分为一个 4 行 5 列的二维数组,其中前 5 个值进入第 1 行,接下来的 5 个值进入第 2 行,依此类推。最终结果应如下所示:

array2D:

[[10, 11, 12, 13, 14]
[20, 21, 22, 23, 24]
[30, 31, 32, 33, 34]
[40, 41, 42, 43, 44]]

实际上,数组会更长(可能超过 100 行),但列数为 5,行数可被 5 整除。例如,我已经简化了。这是我迄今为止尝试过的:

int[] array = { 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31, 32,33, 34,40,41,42,43, 44};
int[,] array2D = new int[(array.Length/5),5];

int count_0 = 1;
int count_1 = 1;
int count_2 = 1;
int count_3 = 1;
int count_4 = 1;

for (int i = 0; i < array.Length; i++)
{
    if (i < 5)
    {
        // works for the first 5 values: 
        array2D[0, i] = array[i];
    }
    // I tried this approach for the rest where I try to say that if Im at index 5, 
    //and every 5th element from here, append to it to index[i,0] of array2D and so on. 
    // This do not work, and is what I need help with.
    else if (i >= 5 && i % 5 == 0)
    {
        array2D[count_0, 0] = array[i];
        count_0++;
    }
    else if (i >= 6 && i % 5 == 0)
    {
        array2D[count_1, 1] = array[i];
        count_1++;
    }

    else if (i >= 7 && i % 5 == 0)
    {
        array2D[count_2, 2] = array[i];
        count_2++;
    }

    else if (i >= 8 && i % 5 == 0)
    {
        array2D[count_3, 3] = array[i];
        count_3++;
    }
    else if (i >= 9 && i % 5 == 0)
    {
        array2D[count_4, 4] = array[i];
        count_4++;
    }
}

当然,对于这个例子,我可以说if &gt; 5 &amp;&amp; &lt;10 {append to array2D}if &gt; 10 &amp;&amp; &lt;15 {append to array2D} 等等,但我想要一个适用于数百个值的大型数组的东西。如果有人有更聪明的方法来做到这一点,请告诉我。

感谢您的帮助!

【问题讨论】:

标签: c# arrays .net winforms


【解决方案1】:

你可以只计算索引:

for(int i=0;i<array.Length;i++)
    array2D[i/5, i%5] = array[i];

【讨论】:

  • 谢谢,这是一个简单的方法!
  • %(mod) 的奇迹
【解决方案2】:

您可以使用Enumerable.GroupBy 来完成LINQ。

array.GroupBy(x => x / 5)
   .Select(x => x.ToArray())
   .ToArray()

【讨论】:

    【解决方案3】:

    您可以使用简单的除法轻松计算索引。该行是通过将 i 除以所需的列号来计算的。该列只是该部门的提醒。

    using System;
    
    public class Program
    {
        public static T[,] SplitInto2DArray<T>(T[] array, int rows, int columns) {      
            T[,] result = new T[rows, columns];
    
            for (int i = 0; i < array.Length; i++) {
                result[i / columns, i % columns] = array[i];    
            }
    
            return result;
        }
    
        public static void PrintArray<T>(T[,] array) {
            for (int i = 0; i < array.GetLength(0); i++) {
                for (int j = 0; j < array.GetLength(1); j++) {
                    Console.Write(array[i, j] + " ");
                }
                Console.WriteLine();
            }
        }
    
        public static void Main()
        {
            int[] array = { 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31, 32, 33, 34, 40, 41, 42, 43, 44};
    
            int[,] splittedArray = SplitInto2DArray(array, 4, 5);
    
            PrintArray(splittedArray);
        }
    }
    

    【讨论】:

    • 虽然您的回答可能会解决问题,但如果您添加一些描述代码的文本会很有帮助。
    • 得到它并添加了一些解释。希望现在没事。我是新来的:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    • 2020-09-23
    • 1970-01-01
    • 2017-09-11
    • 2020-03-21
    • 2017-07-29
    • 2017-03-03
    相关资源
    最近更新 更多