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

发布于 2024-09-10 02:40:23 字数 207 浏览 3 评论 0原文

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

假设我有一个二维数组

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

获取我知道的数字 6 的位置,

现在假设我想通过一维数组 我可以使用 Array.indexOf() 但是什么会我的选择是二维数组?

谢谢!

Well simple question here (maybe not a simple answer?)

Say I have a two dimensional array

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

Now suppose I want to get the position of the number 6

I know with a one-dimensional array i can use Array.indexOf() but what would my options be with 2-dimensional arrays?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ˉ厌 2024-09-17 02:40:23

我会这样说:

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

I'd say something like this:

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);
}
坠似风落 2024-09-17 02:40:23

这是一个应该在数组中查找具有任意排名的索引的方法。

...添加了每个等级的上限/下限范围

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

Here is a method that should find an index in an array with an arbitrary rank.

... Added Upper/Lower bounds range per rank

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文