1

In short I am looking for a way in Java/C# for generation of all possible variations based on different sets containing different values. For example: Lets say we have set of names:

 -Max, Jack, Roger

Second set of verbs

-Loves, hates, knows

And third set of programming languages just as an example but we may have 10 sets

-Java, C#, Python, Visual Basic, C++

What I want is a way to generate all possible variations containing ALL attributes and having all values for example an output should be :

Max loves Java
Jack loves Java
Roger loves Java
Max hates Java
Jack hates Java
Roger hates Java
Max knows Java
Jack knows Java
Roger knows Java
Max loves C#
Jack loves C# 
Roger loves C#
and so on... this will generate 45 variations if I am not wrong at the end

Can anyone help ? I believe a similar easier example will be if you want to generate a variations of products in some clothing shop which have different sizes, colors and materials for example and you want all variations.

3
  • 1
    blogs.msdn.com/b/ericlippert/archive/2010/06/28/… and links seq. Commented Jun 6, 2013 at 15:34
  • 3
    Lots on SO when you search Cartesian Product; pick your favourite dupe :) Commented Jun 6, 2013 at 15:35
  • 5
    Simply loop through each set and append the strings? e.g, something like for i in (Max, Jack, Roger) for j in (loves, hates) for k in (Java, C#) print i+j+k;? Commented Jun 6, 2013 at 15:37

1 Answer 1

4
String[] names={"Max", "Jack", "Roger"};
String[] verbs={"Loves", "hates", "knows"};
String[] languages={"Java", "C#", "Python", "Visual Basic", "C++"};
for(String name:names)
   for(String verb:verbs)
      for(String language:languages)
         System.out.println(name+" "+verb+" "+language);

EDIT:

Oh ,i see . I misunderstood the question . You can just use the magic of recursion to solve this. This is what you want:

public static void main(String[] args) {
    String[][] sets={
            {"Max", "Jack", "Roger"},
            {"Loves", "hates", "knows"},
            {"Java", "C#", "Python", "Visual Basic", "C++"},
    };
    combine(sets,0,"");
}

public static void combine(String[][] list,int index,String upperText)
{
    if(index==list.length)return;
    for(String i:list[index]){
        combine(list,index+1,upperText+i);
        if(index==list.length-1){
            System.out.println(upperText+i);
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

It was a bit more tricky sicne the amount of for loops was not fixed.. I mean the amount of "sets" is not just 3 and I didnt want repercussion ... but anyway I didnt asked correctly so thanks for tha snwer
Yup I agree with recursion you can :) I also found this answer which is in C# but shows good solution as well using the C# magic :) : stackoverflow.com/questions/545703/combination-of-listlistint Anyway I've done it already with recursion :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.