Working with vectors / matrices

Asked

Viewed 82 times

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’ve tried with list, array, matrix.. And nothing works... I’d like to know if you have how.. And how to?

2 answers

4

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

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

On this line you have created a vector, not a matrix, so it is not possible to access one more dimension of this vector. To actually create the matrix, 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 matrix:

string valorMatriz = vetor[0][1]; //Golem

3


Note that your vector variable is not a string array, but an array of objects And to recover the value you need to treat it properly. Or actually use a matrix through the declaration string[][].

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
  • Thank you so much!! I just needed to add a [] in the string[], and all the bugs are gone, and I breaking my head here uhahahah, thank you for your help :)))

Browser other questions tagged

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