我要实现这样的功能:根据给定的二维数组和行号(从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;
}但是运行的时候报错“ 指定的数组必须具有相同的维数”,请问上述程序出在哪里?

解决方案 »

  1.   

    [,] is different, use manual copying
    int []array=new int[cols];for (int i=0; i < cols; i++)
      array[i] = sourceArray(row,i);
      

  2.   

    sorryarray[i] = sourceArray[row,i];
      

  3.   

    Array.Copy()只能复制到相同维数的数组,因为只能用思归老大方法。如果要提高效率可以用非安全代码。
      

  4.   

    saucer(思归),SqlDataAdapter(小鸟)是正确的,这就加分,谢谢啦