简体   繁体   English

在C#中将2维数组合并为2D数组

[英]Combining two single dimensional arrays in a 2D array in c#

I am having two 1D arrays. 我有两个1D阵列。 I want to convert these 2 arrays as single 2D array. 我想将这两个数组转换为单个2D数组。

My code is: 我的代码是:

public Static void Main()
{
int[] arrayRow;
int[] arrayCol;
  for (int i = 0; i < row; i++)
  {
   for (int j = 0; j < column; j++)
     {
       int[,] myArray = new int[row,column];
       myArray[i,j] = arrayRow[i]; // not possible -- your suggestions           
     }
   }
for (int i = 0; i < row; i++)
  {
   for (int j = 0; j < column; j++)
     {
       Console.Write(myArray[i,j]);         
     }
   }
}

I need to save arrayRow[] and arrayCol[] in myArray[,] . 我需要保存arrayRow[]arrayCol[]myArray[,]

For example, 例如,

if we have arrayRow[]={1,2,3} and arrayCol[]={4,5,6} then myArray[,]={(1,4),(2,5),(3,6)} 如果我们有arrayRow[]={1,2,3}arrayCol[]={4,5,6}则myArray [,] = {(1,4),(2,5),(3,6) }

Note: arrayRow and arrayCol may have different lengths. 注: arrayRowarrayCol可能有不同的长度。 In such cases the element that have no pair should be stored in the new single dimensional array result[] . 在这种情况下,不成对的元素应存储在新的一维数组result[]

Your arrayRow[] and arrayCol[] will be just two lines of a two-dimensional array (if you didn't mean jagged one). arrayRow[]arrayCol[]将只是两个二维数组的行(如果你不是故意的锯齿一个)。

So the code to unite two arrays into one is just: 因此,将两个数组组合为一个的代码只是:

public static T[,] Union<T>(T[] first, T[] second) //where T : struct
{
    T[,] result = new T[2, Math.Max(first.Length, second.Length)];
    int firstArrayLength = first.Length * Marshal.SizeOf(typeof(T));
    Buffer.BlockCopy(first, 0, result, 0, firstArrayLength);
    Buffer.BlockCopy(second, 0, result, firstArrayLength, second.Length * Marshal.SizeOf(typeof(T)));
    return result;
}

As it have been mentinoned , BlockCopy is cooler than for cycle. 由于它已经mentinonedBlockCopy是比较冷for循环。


If you do mean that you need a jagged array (like int[][] ), that the solutiona will be way more simplier: 如果确实需要一个锯齿状的数组(例如int[][] ),那么solutiona会更简单:

public static T[][] UnionJagged<T>(T[] first, T[] second)
{
    return new T[2][] { first, second };
}

Which transforms into even simplier if we add multiple-array-as-parameters functionality: 如果我们添加多数组作为参数的功能,这将变得更加简单:

public static T[][] UnionJagged<T>(params T[][] arrays)
{
    return arrays;
}

static void Main()
{
    int[] a = new int[] { 10, 2, 3 };
    int[] b = new int[] { -1, 2, -3 };
    int[] c = new int[] { 1, -2, 3 };
    int[][] jaggedThing = UnionJagged(a, b, c);
}

Didn't tryed this, and I'm just guessing what you want to acomplish, but here it is: 没有尝试过,我只是在猜测您要完成什么,但这是:

int[] arrayRow;
int[] arrayCol;

int[,] myArray = new int[Math.Max(arrayRow.Length, arrayCol.Length), 2];

for (int i = 0; i < arrayRow.Length; i++)
  myArray[i, 0] = arrayRow[i];

for (int i = 0; i < arrayCol.Length; i++)
  myArray[i, 1] = arrayCol[i];

More performant / another way: 性能更高/另一种方式:

public static void ConvertFlatArrayToMatrix(int[] array, int[,] matrix, int dimension) {
        for(int i = 0; i < array.Length; i++) {
            int r = Mathf.FloorToInt(i / dimension);
            int c = i % dimension;

            matrix[c,r] = array[i];
        }
    }

This will just push the result into a 2d array you pass in. 这只会将结果推入您传入的2d数组中。

Keep in mind, I'm not checking length or protecting against anything here, this is just the concept and the bare minimum to get the job done. 请记住,这里我不检查长度或进行任何保护,这只是概念,是完成工作的最低要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM