我要实现这样的功能:根据给定的二维数组和行号(从0开始),将该行的数据复制到一个新的数组之中,我是这样实现的:
public int[] GetRowFromMultiArray(int [,]sourceArray,int row)
{
//参数row是sourceArray中的某一行(注意:row从0开始计算)
//sourceArray.
int rows=sourceArray.GetLength(0);
int cols=sourceArray.GetLength(1); //判断参数row有没有超出范围,若超出,产生异常
if (row>=rows)
{
//抛出异常
new Exception("超出数组范围!");
//return;
}
//没有超出,首先计算第row行的起止index
int beginIndex=0; beginIndex=row*cols;
int []array=new int[cols];
Array.Copy(sourceArray,beginIndex,array,0,cols);
return array;
}但是运行的时候报错“ 指定的数组必须具有相同的维数”,请问上述程序出在哪里?
public int[] GetRowFromMultiArray(int [,]sourceArray,int row)
{
//参数row是sourceArray中的某一行(注意:row从0开始计算)
//sourceArray.
int rows=sourceArray.GetLength(0);
int cols=sourceArray.GetLength(1); //判断参数row有没有超出范围,若超出,产生异常
if (row>=rows)
{
//抛出异常
new Exception("超出数组范围!");
//return;
}
//没有超出,首先计算第row行的起止index
int beginIndex=0; beginIndex=row*cols;
int []array=new int[cols];
Array.Copy(sourceArray,beginIndex,array,0,cols);
return array;
}但是运行的时候报错“ 指定的数组必须具有相同的维数”,请问上述程序出在哪里?
int []array=new int[cols];for (int i=0; i < cols; i++)
array[i] = sourceArray(row,i);