Reputation: 165
I need to simulate a Matrix of data and Im using a List<List <string >>
.
Im using the IndexOf
to search an element in a list of the list.
Matrix[0].IndexOf('Search');
But is it possible to make a kind of IndexOf
in Matrix
?
Upvotes: 0
Views: 333
Reputation: 57220
You could use FindIndex
method:
int index = Matrix.FindIndex(x => x[colIndex] == "Search");
This method is obviously useful if you want to search the row index by knowing the column to search in.
If you want to search in the whole matrix you could write a simple method:
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);
}
Upvotes: 2
Reputation: 62276
You may be are asking for something like :
Example
public void MatrixIndexOf(string content) {
var result = matrix.Select((value, index)=> new {
_i = index,
_str = value
}).Where(x=>x._str.Contains(content));
}
after this result
is anonymous type where_i
is index.
Upvotes: 0
Reputation: 68707
You'd have to make your own class to achieve it.
public class Matrix<T>
{
public void IndexOf<T>(T value, out int x, out int y){...}
}
or use an extension on your type
public static void IndexOf<T>(this List<List<T>> list, out int x, out int y){...}
Personally, I'd make the extension on a 2 dimensional array rather than List<List<T>>
.
Upvotes: 1
Reputation: 50752
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;
}
Upvotes: 0