在C#中将两个一维数组合并为2D数组 [英] Combining two single dimensional arrays in a 2D array in c#

查看:118
本文介绍了在C#中将两个一维数组合并为2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个一维数组.我想将这2个数组转换为单个2D数组.

I am having two 1D arrays. I want to convert these 2 arrays as single 2D array.

我的代码是:

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]);         
     }
   }
}

我需要将arrayRow[]arrayCol[]保存在myArray[,]中.

I need to save arrayRow[] and arrayCol[] in myArray[,].

例如,

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

if we have arrayRow[]={1,2,3} and arrayCol[]={4,5,6} then myArray[,]={(1,4),(2,5),(3,6)}

注意:arrayRowarrayCol的长度可能不同.在这种情况下,不成对的元素应存储在新的一维数组result[]中.

Note: arrayRow and arrayCol may have different lengths. In such cases the element that have no pair should be stored in the new single dimensional array result[].

推荐答案

没有尝试过,我只是在猜测您要完成什么,但这是:

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];

这篇关于在C#中将两个一维数组合并为2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆