我需要模拟矩阵的数据和Im使用List<List <string >>
。 Im使用IndexOf
来搜索列表中的元素。矩阵与列表<列表<string >>索引
Matrix[0].IndexOf('Search');
但有可能使一种IndexOf
在Matrix
?
我需要模拟矩阵的数据和Im使用List<List <string >>
。 Im使用IndexOf
来搜索列表中的元素。矩阵与列表<列表<string >>索引
Matrix[0].IndexOf('Search');
但有可能使一种IndexOf
在Matrix
?
for(int i = 0; i<Matrix.Length; i++)
for(int j = 0; j<Matrix.Length; j++)
if(Matrix[i][j] == "Search")
{
//OUT i,j;
return;
}
你必须让自己的班级来实现它。
public class Matrix<T>
{
public void IndexOf<T>(T value, out int x, out int y){...}
}
或使用你的类型的扩展
public static void IndexOf<T>(this List<List<T>> list, out int x, out int y){...}
就个人而言,我会成为一个二维阵列上,而不是List<List<T>>
扩展。如果您想了解该列中进行搜索,搜索的行索引
int index = Matrix.FindIndex(x => x[colIndex] == "Search");
这种方法显然是非常有益
如果你想在全搜索:
你可以使用FindIndex
方法。矩阵你可以写一个简单的方法:
public static Tuple<int,int> PositionOf<T>(this List<List<T>> matrix, T toSearch)
{
for (int i = 0; i < matrix.Count; i++)
{
int colIndex = matrix[i].IndexOf(toSearch);
if (colIndex >= 0 && colIndex < matrix[i].Count)
return Tuple.Create(i, colIndex);
}
return Tuple.Create(-1, -1);
}
您可能都在问类似:
例
public void MatrixIndexOf(string content) {
var result = matrix.Select((value, index)=> new {
_i = index,
_str = value
}).Where(x=>x._str.Contains(content));
}
这result
后是匿名的类型,其中_i
是指数。
index传统上返回单个数字。这样的功能在二维矩阵上是没有意义的。让我们说你想要一个位置返回列表的索引,然后是列表中的字符串的索引,你可以编写自己的扩展方法,它循环遍历列表,然后执行和索引以给你想要的结果。 – ryadavilli