How to get the row and column indexes of a list List<List<int>>?

Asked

Viewed 888 times

4

I have a list that simulates a matrix (or two-dimensional array) that is structured as follows:

List<List<int>> matriz = new List<List<int>>();

Whereas this matriz is initialized with the following values:

matriz.Add(new List<int> {3, 4, 1}  );
matriz.Add(new List<int> {2, 4, 5}  );
matriz.Add(new List<int> {44, 8, 9} );

And my problem is finding the position of line and of spine given a given value. For example, the value 44 is on the line 2 and column 0, see:

matriz[2][0] // 44

I tried to use the method FindIndex in this way:

matriz.FindIndex(x => x.Contains(44))

However, it only returns me a single value which is the position corresponding to the line which is 2, and I would need the line and column position 2 and 0.

Question

  • How could I get the indexes corresponding to row and column given a certain value of a list List<List<int>>?

2 answers

1

You can browse the list using ForEach to discover the column:

int a = new int(),
    b = new int(),
    busca = 1;
a = matriz.FindIndex(x => x.Contains(busca));
matriz.ForEach(x => { if(x.Contains(busca)){ b = x.IndexOf(busca); } });
Console.WriteLine("Busca: {0}, Linha: {1}, Coluna: {2}", busca, a, b);

You can see it working in ideone

Or use the for:

int busca = 1;
for(int a = 0; a < matriz.Count; a++) {
  for(int b = 0; b < matriz[a].Count; b++) {
    if (matriz[a][b] == busca) {
      Console.WriteLine("Busca: {0}, Linha: {1}, Coluna: {2}", busca, a, b);
    }
  }
}

You can see it working in ideone

  • What if you have other items with the same item? It will cause confusion on Linq.

0

Try this:

    static int GetIndex(List<List<int>> array, int line, int column)
    {
        // índice atual
        int temp_index = 0;
        // percorre nas linhas até onde você quer ir
        for(int x = 0; x <= line; x++)
        {
            // se for a linha desejada, percorre nas colunas dela
            if(x == line)
            {
                for(int y = 0; y <= column; y++)
                {
                    temp_index++; /* irá parar sozinho quando chegar na coluna desejada */
                }
            } else temp_index += array[x].Count; /* adiciona as colunas no índice */
        }
        return temp_index - 1; /* retorna o índice zero-based index */
    }

This method will scroll through your matrix, and does not need to have a fixed size for each line, so each line can have x elements that will not interfere in the final index.

Note: remove the - 1 at the end if you do not want a zero-position index.

Mode of use:

    static void Main(string[] args)
    {
        List<List<int>> matriz = new List<List<int>>();
        matriz.Add(new List<int> { 3, 4, 1 });
        matriz.Add(new List<int> { 2, 4, 5 });
        matriz.Add(new List<int> { 44, 8, 9 });

        Console.WriteLine(GetIndex(matriz, 1, 1)); // 4, index 4
        Console.WriteLine(GetIndex(matriz, 2, 0)); // 44, index 6
        Console.WriteLine(GetIndex(matriz, 0, 3)); // 1, index 3

        Console.ReadLine();
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.