seb*_*ebu 4 c# linq arrays matrix
我正在使用两个2D数组,实际上:
MatrixAddition(int [] [] a,int [] [] b)
使用LINQ添加两个矩阵并将它们返回到2D数组格式int[][].LINQ结果没问题,返回了预期结果,但无法帮助自己以int [] []格式返回它们.
矩阵加法()
public static int[][] MatrixAddition(int[][] a, int[][] b)
{
return (int[][])a.Select((x, i) => x.Select((y, j) => a[i][j] + b[i][j]));
}
Run Code Online (Sandbox Code Playgroud)
错误: System.InvalidCastException:'无法转换类型为'd__5 2[System.Int32[],System.Collections.Generic.IEnumerable1 [System.Int32]]'的对象以键入'System.Int32 [] []'.'
没有强制转换的当前代码会返回嵌套在另一个可枚举数中的枚举数.您需要将内部枚举和外部枚举转换为int[],并删除强制转换:
return a.Select(
(x, i) => x.Select((y, j) => a[i][j] + b[i][j]).ToArray()
).ToArray();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
102 次 |
| 最近记录: |