Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try something like this:</p> <pre><code>public static void generate(int[][] sets) { int solutions = 1; for(int i = 0; i &lt; sets.length; solutions *= sets[i].length, i++); for(int i = 0; i &lt; solutions; i++) { int j = 1; for(int[] set : sets) { System.out.print(set[(i/j)%set.length] + " "); j *= set.length; } System.out.println(); } } public static void main(String[] args) { generate(new int[][]{{1,2,3}, {3,2}, {5,6,7}}); } </code></pre> <p>which will print:</p> <pre><code>1 3 5 2 3 5 3 3 5 1 2 5 2 2 5 3 2 5 1 3 6 2 3 6 3 3 6 1 2 6 2 2 6 3 2 6 1 3 7 2 3 7 3 3 7 1 2 7 2 2 7 3 2 7 </code></pre> <p>I've implemented the algorithm above based on (I believe) one of Knuth's <a href="http://www-cs-staff.stanford.edu/~uno/taocp.html" rel="nofollow noreferrer">TAOCP</a> books (in the comments @chikitin has a more specific reference: it is in PRE FASCICLE 2A section 7.2.1.1 Generating All n-tuple, of The Art Of Computer Programming by Knuth, Addison Wesley).</p> <p>Note that I've named the arrays <code>set</code>, but they needn't hold unique elements, of course. The time I used it, they did contain unique elements, hence the name.</p> <h2>EDIT</h2> <p>It's pretty much a 1-on-1 translation:</p> <pre><code>import java.util.Arrays; import java.util.LinkedHashMap; import java.util.Vector; public class Foo { private LinkedHashMap&lt;String, Vector&lt;String&gt;&gt; dataStructure; public Foo(LinkedHashMap&lt;String, Vector&lt;String&gt;&gt; dataStructure){ this.dataStructure = dataStructure; } public String[][] allUniqueCombinations(){ int n = dataStructure.keySet().size(); int solutions = 1; for(Vector&lt;String&gt; vector : dataStructure.values()) { solutions *= vector.size(); } String[][] allCombinations = new String[solutions + 1][]; allCombinations[0] = dataStructure.keySet().toArray(new String[n]); for(int i = 0; i &lt; solutions; i++) { Vector&lt;String&gt; combination = new Vector&lt;String&gt;(n); int j = 1; for(Vector&lt;String&gt; vec : dataStructure.values()) { combination.add(vec.get((i/j)%vec.size())); j *= vec.size(); } allCombinations[i + 1] = combination.toArray(new String[n]); } return allCombinations; } public static void main(String[] args) { LinkedHashMap&lt;String, Vector&lt;String&gt;&gt; data = new LinkedHashMap&lt;String, Vector&lt;String&gt;&gt;(); data.put("foo", new Vector&lt;String&gt;(Arrays.asList("1", "2", "3"))); data.put("bar", new Vector&lt;String&gt;(Arrays.asList("3", "2"))); data.put("baz", new Vector&lt;String&gt;(Arrays.asList("5", "6", "7"))); Foo foo = new Foo(data); for(String[] combination : foo.allUniqueCombinations()) { System.out.println(Arrays.toString(combination)); } } } </code></pre> <p>If you run the class above, the following is printed:</p> <pre><code>[foo, bar, baz] [1, 3, 5] [2, 3, 5] [3, 3, 5] [1, 2, 5] [2, 2, 5] [3, 2, 5] [1, 3, 6] [2, 3, 6] [3, 3, 6] [1, 2, 6] [2, 2, 6] [3, 2, 6] [1, 3, 7] [2, 3, 7] [3, 3, 7] [1, 2, 7] [2, 2, 7] [3, 2, 7] </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload