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

10

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

假设我有一个二维数组

[0] [1] [2]
[3] [4] [5]
[6] [7] [8]

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

我知道对于一维数组可以使用 Array.indexOf() 方法,但是对于二维数组该如何操作呢?

谢谢!


我假设你指的是二维数组(int[,]),而不是交错数组(int[][])。 - SLaks
2个回答

22
我会这样说:

我会这样说:

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);
}

1
在这种情况下,我更喜欢创建一个类来代表结果,而不是使用元组,因为元组不能传达返回的内容。当然,当您查看行Tuple<int,int> coordinate = matrix.CoordinatesOf(5)时,您可以轻松猜测它是一个坐标/点,但一旦该值穿过系统,它就变成了一个元组,留下一些可怜的开发人员来追踪源代码以了解Tuple<int,int>实际表示什么。 - P. Roe

1
这里有一个方法,应该可以在任意维度的数组中找到一个索引。
...为每个维度添加了上限/下限范围。
public static class Tools
{
    public static int[] FindIndex(this Array haystack, object needle)
    {
        if (haystack.Rank == 1)
            return new[] { Array.IndexOf(haystack, needle) };

        var found = haystack.OfType<object>()
                          .Select((v, i) => new { v, i })
                          .FirstOrDefault(s => s.v.Equals(needle));
        if (found == null)
            throw new Exception("needle not found in set");

        var indexes = new int[haystack.Rank];
        var last = found.i;
        var lastLength = Enumerable.Range(0, haystack.Rank)
                                   .Aggregate(1, 
                                       (a, v) => a * haystack.GetLength(v));
        for (var rank =0; rank < haystack.Rank; rank++)
        {
            lastLength = lastLength / haystack.GetLength(rank);
            var value = last / lastLength;
            last -= value * lastLength;

            var index = value + haystack.GetLowerBound(rank);
            if (index > haystack.GetUpperBound(rank))
                throw new IndexOutOfRangeException();
            indexes[rank] = index;
        }

        return indexes;
    }
}

这段代码非常简洁,但是 ToList 调用让我觉得有点懒惰...为什么要将多维数组的整个内容复制到 List<T> 中,只为了使用 IndexOf?你可以编写自己的代码,在原地枚举数组。 - Dan Tao
好主意。我本来就在考虑为范围边界添加支持,所以我会改变那个的。 - Matthew Whited

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接