如何在C#中获取给定的数组

时间:2017-04-20 07:16:28

标签: c# arrays

给定R = (R_1, R_2,..., R_n) 如何在n*(2n+2)中获取这样的C#数组,注意数组不是List,n非常大。

enter image description here

1 个答案:

答案 0 :(得分:4)

以下方法描述了您想要的结果:

double GetValue(int row, int col, double[]firstrow)
{
    if( row == 0)
        return firstrow[col];
    if( row == 1)
        return 1;
    if (row - 2 == col || row - firstrow.Length - 2 == col)
        return 1;
    else
        return 0;
}

您可以循环使用此方法来填充数组:

var firstRow = new double[]{2.3, 4.3, 5.8};// example input
int n = firstRow.Length;
var result = new double[2*n+2, n]
for(int row = 0; row < 2*n+2; row++)
{
    for( int col = 0; col < n; col++) 
    {
        result[row, col] = GetValue(row,col,firstrow);
    }
}

但是,如果firstRow很大,则生成的n*(2n+2)可能太大而无法使用。在这种情况下,您可能希望在需要值时直接调用double GetValue(int row, int col, double[]firstrow)来替换展开的数组。