-1

I have several groups, each having multiple properties. For e.g. G1 (p11, p12, p13 ...) G2 (p21, p22, p23, ...) and so on.

I want to create all possible combination of properties like (p11, p21), (p12, p21), (p13, p21) and so on.

How to do this in Java ? Can anyone point me to an algorithm ?

Is it equivalent to finding the cartesian product of the groups (sets)

Sourajit Basak
  • 693
  • 5
  • 15

2 Answers2

0

try looping inside a loop

 for (String x : G1) {
    for (String y : g2) {
       System.out.println (String.format ("%s,%s", x, y));
    }
 }
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

use recursion

if your sets are s[1], s[2] ... s[n] something like:

 void rec(int p) {
     if (p > n ) 
        return;
     for ( x in s[p] ) {
         //print or add x to current combination
         rec(p+1);
     }
 }

and then call

rec(1);
Herokiller
  • 2,891
  • 5
  • 32
  • 50