在二维数组中查找元素的位置?
这里简单的问题(也许不是一个简单的答案?)
说我有一个二维数组
[0] [1] [2] [3] [4] [5] [6] [7] [8]
现在假设我想获得数字6的位置
我知道使用一维数组我可以使用Array.indexOf()但是我的选择是什么?二维数组?
谢谢!
我会说这样的话:
public static Tuple CoordinatesOf(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); }
这是一个应该在具有任意排名的数组中找到索引的方法。
…添加每个等级的上/下界范围
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