How do I get the row and column contents of a ListListint list?

4

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

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

Since 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 the line and column given a given value. For example, the 44 value is in the 2 line and the 0 column, see:

matriz[2][0] // 44

I tried to use the FindIndex method this way:

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

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

Question

  • How could I get the indices corresponding to row and column given a given value from a list List<List<int>> ?
asked by anonymous 11.04.2018 / 04:06

2 answers

1

You can scroll the list using ForEach for find 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 at ideone

Or use 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 at ideone

    
11.04.2018 / 05:44
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 go through your array, and does not need to have a fixed length for each row, so each row can have x elements that will not break into the final index.

  

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

How to 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();
    }
    
11.04.2018 / 05:59