Getting a double[] row array of a double[,] rectangular array

后端 未结 6 1831
夕颜
夕颜 2021-02-13 13:43

Suppose you have an array like:

double[,] rectArray = new double[10,3];

Now you want the fouth row as a double[] array of 3 elements without do

6条回答
  •  猫巷女王i
    2021-02-13 14:29

    If you must use a rectangular array and just want to simplify the syntax, you can use a method to get the row like so:

    double[] fourthRow = GetRow(rectArray, 3);
    
    public static T[] GetRow(T[,] matrix, int row)
    {
        var columns = matrix.GetLength(1);
        var array = new T[columns];
        for (int i = 0; i < columns; ++i)
            array[i] = matrix[row, i];
        return array;
    }
    

提交回复
热议问题