Working with vectors / arrays

0

Hello, I have the following question, 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 have tried with list, array, array .. And nothing works ... I would like to know if it has how .. And how?

    
asked by anonymous 09.04.2018 / 22:37

2 answers

3

Note that your vector variable is not a string array, but an array of objects And in order to recover the value, it is necessary to treat it properly. Or actually 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
    
09.04.2018 / 22:50
4

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

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

In this line you have created a vector, not an array, so you can not access one more dimension of that vector. To actually create the array, it would look 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 Golem element of the array:

string valorMatriz = vetor[0][1]; //Golem
    
09.04.2018 / 22:49