如何将多维(2D)数组按行分隔成多个一维数组?

时间:2021-09-16 21:18:16

I am programming in C# and I currently have the 2D array below:

我正在用c#编程,目前我有以下2D数组:

int[,] results = {                                  
{ 4, 7, 9, 3, 8, 6, 4},                                  
{ 4, 8, 6, 4, 8, 5, 6},                                  
{ 7, 3, 9, 2, 2, 1, 8}    
};

I'd like to create a loop or function which outputs 3 separate arrays, copying the values from each row.

我想创建一个循环或函数,它输出3个独立的数组,从每一行复制值。

e.g. output:

如输出:

row1 = {4, 7, 9, 3, 8, 6, 4}    
row2 = {4, 8, 6, 4, 8, 5, 6}

etc

I have been able to copy the values of each row into a separate string which is then written in the console with this line of code:

我能够将每一行的值复制到一个单独的字符串中,然后用这一行代码在控制台中编写该字符串:

for (int a = 1; a < (rows+1); a++)                
{                    
    for (int b = 1; b < (columns+1); b++)                    
    {                        
        scores = scores + " " + results[(a-1), (b-1)];                    
    }                    
    scores = "";                
}

3 个解决方案

#1


3  

you need a convertion from a 2D array into a jagged array

您需要从2D数组转换为交错数组

int[,] array = {  { 4, 7, 9, 3, 8, 6, 4},
                    { 4, 8, 6, 4, 8, 5, 6},
                    { 7, 3, 9, 2, 2, 1, 8}
                };            

int[][] jagged = new int[array.GetLength(0)][];
for (int i = 0; i < array.GetLength(0); i++)
{
    int[] row = new int[array.GetLength(1)];
    for (int j = 0; j < array.GetLength(1); j++)
    {
        row[j] = array[i, j];
    }
    jagged[i] = row;
}

int[] row1 = jagged[0];
int[] row2 = jagged[1];
int[] row3 = jagged[2];

difference between multidimensional array and jagged array:

多维数组与锯齿数组的差异:

//multidimensional array
int[,] multi = { { 7, 2, 6, 1 }, { 3, 5, 4, 8 } };
//array of arrays (jagged array) 
int[][] jagged = new int[][] { new int[] { 7, 2, 6, 1 }, new int[] { 3, 5, 4, 8 } };

#2


1  

LINQ variant:

LINQ变体:

var m0 = new int[,] { { 1, 2 }, { 3, 4 } };
var rows = Enumerable.Range(0, m0.GetLength(0)).Select(i => Enumerable.Range(0, m0.GetLength(1)).Select(j => m0[i, j]));
foreach (var r in rows) Console.WriteLine(string.Join(" ", r));
Console.ReadLine();

a 'smarter' variant (flattern an array and take by N values):

一种“更智能”的变体(简化数组并取N个值):

var rows = m0.Cast<int>().Select((v, i) => new { i = i / m0.GetLength(1), v }).GroupBy(e => e.i).Select(g => g.Select(e => e.v));

#3


0  

Just a piece of cake using JsonSoft as

用JsonSoft as做的蛋糕

var json = JsonConvert.SerializeObject(results);
int[][] arr = JsonConvert.DeserializeObject<int[][]>(json);

int[] arr1 = arr[0];
int[] arr2 = arr[1];
int[] arr3 = arr[2];

#1


3  

you need a convertion from a 2D array into a jagged array

您需要从2D数组转换为交错数组

int[,] array = {  { 4, 7, 9, 3, 8, 6, 4},
                    { 4, 8, 6, 4, 8, 5, 6},
                    { 7, 3, 9, 2, 2, 1, 8}
                };            

int[][] jagged = new int[array.GetLength(0)][];
for (int i = 0; i < array.GetLength(0); i++)
{
    int[] row = new int[array.GetLength(1)];
    for (int j = 0; j < array.GetLength(1); j++)
    {
        row[j] = array[i, j];
    }
    jagged[i] = row;
}

int[] row1 = jagged[0];
int[] row2 = jagged[1];
int[] row3 = jagged[2];

difference between multidimensional array and jagged array:

多维数组与锯齿数组的差异:

//multidimensional array
int[,] multi = { { 7, 2, 6, 1 }, { 3, 5, 4, 8 } };
//array of arrays (jagged array) 
int[][] jagged = new int[][] { new int[] { 7, 2, 6, 1 }, new int[] { 3, 5, 4, 8 } };

#2


1  

LINQ variant:

LINQ变体:

var m0 = new int[,] { { 1, 2 }, { 3, 4 } };
var rows = Enumerable.Range(0, m0.GetLength(0)).Select(i => Enumerable.Range(0, m0.GetLength(1)).Select(j => m0[i, j]));
foreach (var r in rows) Console.WriteLine(string.Join(" ", r));
Console.ReadLine();

a 'smarter' variant (flattern an array and take by N values):

一种“更智能”的变体(简化数组并取N个值):

var rows = m0.Cast<int>().Select((v, i) => new { i = i / m0.GetLength(1), v }).GroupBy(e => e.i).Select(g => g.Select(e => e.v));

#3


0  

Just a piece of cake using JsonSoft as

用JsonSoft as做的蛋糕

var json = JsonConvert.SerializeObject(results);
int[][] arr = JsonConvert.DeserializeObject<int[][]>(json);

int[] arr1 = arr[0];
int[] arr2 = arr[1];
int[] arr3 = arr[2];