在二维数组中找到元素的位置?

zet*_*eta 7 c# vb.net arrays indexof multidimensional-array

这里简单的问题(也许不是一个简单的答案?)

说我有一个二维数组

[0] [1] [2]
[3] [4] [5]
[6] [7] [8]
Run Code Online (Sandbox Code Playgroud)

现在假设我想获得数字6的位置

我知道使用一维数组我可以使用Array.indexOf()但是我的选择是什么?二维数组?

谢谢!

Dan*_*Tao 20

我会说这样的话:

public static Tuple<int, int> CoordinatesOf<T>(this T[,] matrix, T value)
{
    int w = matrix.GetLength(0); // width
    int h = matrix.GetLength(1); // height

    for (int x = 0; x < w; ++x)
    {
        for (int y = 0; y < h; ++y)
        {
            if (matrix[x, y].Equals(value))
                return Tuple.Create(x, y);
        }
    }

    return Tuple.Create(-1, -1);
}
Run Code Online (Sandbox Code Playgroud)