Working with vectors / matrices

Hello, I have the following doubt, I have 5 vectors

string[] Defesa = { "Gigante", "Golem", "Gigante Real" };

string[] AtkTorre = { "Corredor", "Ariete de Batalha", "Gigante Real" };

string[] AP = { "Bárbaros de Elite", "Gigante Real", "P.E.K.K.A" };

string[] Sup = { "Bruxa", "Príncipe", "Mosqueteira", "Três Mosqueteiras", "Bruxa Sombria", "Lançador", "Caçador", "Executor", "Carrinho de Canhão" };

string[] C = { "Torre Inferno", "Canhão" };

I would like to access them from another vector, for example:

string[] vetor = { Defesa, AtkTorre, AP, Sup, C };
string valor = vetor[0][1]; // Golem

I've tried with list, array, array.. And nothing works... I wonder if you have how.. And how?

Author: Isac, 2018-04-09

2 answers

Note that your vector variable is not an array string, but rather an array of objects And to recover the value you need to treat it properly. Or in fact use an array through the string[][] statement.

string[] Defesa = { "Gigante", "Golem", "Gigante Real" };
string[] AtkTorre = { "Corredor", "Ariete de Batalha", "Gigante Real" };
string[] AP = { "Bárbaros de Elite", "Gigante Real", "P.E.K.K.A" };
string[] Sup = { "Bruxa", "Príncipe", "Mosqueteira", "Três Mosqueteiras", "Bruxa Sombria", "Lançador", "Caçador", "Executor", "Carrinho de Canhão" };
string[] C = { "Torre Inferno", "Canhão" };

object[] vetor = { Defesa, AtkTorre, AP, Sup, C };
string valor = ((vetor[0] as string[])[1]); //Golem

string[][] matriz = { Defesa, AtkTorre, AP, Sup, C };
string valorMatriz = matriz[0][1]; //Golem
 3
Author: Leandro Angelo, 2018-04-09 20:55:54

What you want to do is in fact a Matrix data structure. What you need to understand better is that access to the array should be done not through the name of the vector of that position, but rather through the vector itself.

string[] vetor = { Defesa, AtkTorre, AP, Sup, C };

In this line you created a vector, not an array, so you cannot access one more dimension of that vector. To create the array in fact, it would be something like:

string[][] vetor = { { "Gigante", "Golem", "Gigante Real" }, { "Corredor", "Ariete de Batalha", "Gigante Real" }, { "Bárbaros de Elite", "Gigante Real", "P.E.K.K.A" }, { "Bruxa", "Príncipe", "Mosqueteira", "Três Mosqueteiras", "Bruxa Sombria", "Lançador", "Caçador", "Executor", "Carrinho de Canhão" }, { "Torre Inferno", "Canhão" } };

With this, you could access the element Golem of the array:

string valorMatriz = vetor[0][1]; //Golem
 4
Author: Giuliana Bezerra, 2018-04-09 20:56:51