这是一个简单的问题(也许答案并不简单?)
假设我有一个二维数组
[0] [1] [2]
[3] [4] [5]
[6] [7] [8]
现在假设我想获取数字 6 的位置。
我知道对于一维数组可以使用 Array.indexOf() 方法,但是对于二维数组该如何操作呢?
谢谢!
这是一个简单的问题(也许答案并不简单?)
假设我有一个二维数组
[0] [1] [2]
[3] [4] [5]
[6] [7] [8]
现在假设我想获取数字 6 的位置。
我知道对于一维数组可以使用 Array.indexOf() 方法,但是对于二维数组该如何操作呢?
谢谢!
我会这样说:
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);
}
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
int[,]
),而不是交错数组(int[][]
)。 - SLaks