2
I’m having a hard time doing this.
Supposing I have two objects of the kind Person, which has the attributes name and employment.
P1 (name=John, job=Taxi driver)
P2 (name=Maria, job=Programmer)
Then I would like to generate the possible combinations of these two objects, of course without attributes errors, for example an object having for example the name Taxi driver.
The way out I would need in case.
P3 (name=John, job=Programmer)
P4 (name=Maria, job=Taxi driver)
How can I do that? There are libraries that allow me?
EDIT
An example of code would look something like this:
...
Pessoa p1 = new Pessoa("João","Taxista");
Pessoa p2 = new Pessoa("Maria", Programador;
List<Pessoa> listaDePessoas = new ArrayList<Pessoa>;
listaDePessoas.add(p1);
listaDePessoas.add(p2);
List<Pessoa> novaLista = new ArrayList<Pessoa>;
novaLista = geraCombinacoes(listaDePessoas); //esse seria o método por exemplo.
Ai when printing that list the output would be:
(João, Taxista)
(Maria, Programador)
(João, Programador)
(Maria, Taxista)
I couldn’t figure out what you’re looking for. What combinations are you talking about? Put some code showing where you’re going.
– Maniero
I don’t quite understand your doubt... as far as I can tell, you need an object like
Pessoaand an object of the typeEmprego, where the Person could be instantiated with different names (John, Mary, etc.) and could be related to some Employment (Programmer, Taxi driver, etc.).– Dherik
Hello @bigown, explaining better, let’s assume that I have a list with 2 objects that are instances of my person class, in case they are part of this list P1 and P2 objects. I want to pass this list as a parameter for a method, and it returns me the possible combinations, which I mentioned above, the combination would be between the attributes of the objects to create new objects. I’ll put it as code, see if it gets better.
– João Neto
It’s a simple combination algorithm, right? You want everyone to have all the trades?
– cantoni
This @Cantoni, I want everyone to have all the professions, and other tmb attributes, if there are other attributes, they also enter the combination.
– João Neto
Right. I made an algorithm here with just these two attributes. It’s so simple that I don’t think that’s what you want. It is a quadratic algorithm (one is inside the other). If you want me to post. The version for arbitrary attributes needs to use Reflection.
– cantoni
@Cantoni, can post, any help is valid, anything I try to develop more, is already a start! But really, where I will actually use I have a 6 attributes. I put only 2 to facilitate the explanation.
– João Neto
I put a code to illustrate better. I don’t know if I understand better. In this case, all people have to have all the professions. In case you had an attribute called: address. Every person would have to have every job and every address, in this case, every possible combination.
– João Neto
Do you want something generic or will always be these 6 attributes?
– cantoni
For now it will be only those six attributes, but in the future it may have to change. Something generic would be great, but if only the 6 attributes remain, it’s still great!
– João Neto