10

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!

1
  • I assume that you mean a 2D array (int[,]), not a jagged array (int[][])
    – SLaks
    Jul 15, 2010 at 23:58

2 Answers 2

22

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);
}
1
  • 1
    In situations like this I prefer to create a class to represent the result rather than use a Tuple since Tuple doesn't convey what is being returned. Sure when you're looking at the line Tuple<int,int> coordinate = matrix.CoordinatesOf(5) you can easily guess that it's a coordinate/point but once that value moves through the system it becomes just a Tuple leaving some poor dev to track down the source to learn what Tuple<int,int> actually represents.
    – P. Roe
    Oct 28, 2016 at 17:59
1

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;
    }
}
2
  • This is very clean, but the ToList call strikes me as a bit lazy... why copy the entire contents of a multidimensional array to a List<T> only to use IndexOf? You can write your own that enumerates over the array in-place.
    – Dan Tao
    Jul 16, 2010 at 1:29
  • good point. I was thinking about adding support to the range bounds anyway for I will change that. Jul 16, 2010 at 1:33

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.