c# - 如何在 C# 中从二维数组中获取完整的行或列

标签 c# multidimensional-array

我不想使用锯齿状数组,我有一个二维数组,我想在不循环遍历的情况下获得完整的列或行。有没有人知道如何做到这一点。

double [,]  array = new double [3,3] ;

1   2   3 
4   5   6

Out: 1   2   3  or 2   5 

最佳答案

要从多维数组中获取特定的行或列,您可以使用一些 LINQ:

public class CustomArray<T>
{
    public T[] GetColumn(T[,] matrix, int columnNumber)
    {
        return Enumerable.Range(0, matrix.GetLength(0))
                .Select(x => matrix[x, columnNumber])
                .ToArray();
    }

    public T[] GetRow(T[,] matrix, int rowNumber)
    {
        return Enumerable.Range(0, matrix.GetLength(1))
                .Select(x => matrix[rowNumber, x])
                .ToArray();
    }
}

关于c# - 如何在 C# 中从二维数组中获取完整的行或列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27427527/

相关文章:

c# - 在 Windows Mobile 6 上实现 GetDeviceUniqueID 时遇到问题

php数组组合

arrays - 动态填充多维 awk 数组

C# RegEx - 查找 html 标签(div 和 anchor )

c# - 在 C# 中是否存在值增加的 numericupdown 事件

javascript - Controller 收到每个页面请求 2 个请求

c# - 如何将其他命名空间导入 Visual C#?

c - C 函数原型(prototype)返回二维数组

mysql - 将多个数组合并为单个数组

python - 基于文本的游戏的多维数组