title: How to create cartesian product over arbitrary groups of numbers in Java?
body: <p>Let's say I have 2 groups of numbers:</p>
  
  <pre><code>{1, 2, 3},
  {4, 5}
  </code></pre>
  
  <p>I'd like to create an algorithm (in Java) that outputs the following 6 combinations:</p>
  
  <pre><code>1,4
  1,5
  2,4
  2,5
  3,4
  3,5
  </code></pre>
  
  <p>There can be an arbitrary number of groups and an arbitrary number of members within each group. So in the above example, there are 2 groups with the first group having 3 members and the second group having 2 members. Another example is the following (3 groups, 3 members in first groups and 2 members in second and third groups):</p>
  
  <pre><code>{1, 2, 3},
  {4, 5},
  {6, 7}
  </code></pre>
  
  <p>Which would yield the following 12 combinations:</p>
  
  <pre><code>1,4,6
  1,4,7
  1,5,6
  1,5,7
  
  2,4,6
  2,4,7
  2,5,6
  2,5,7
  
  3,4,6
  3,4,7
  3,5,6
  3,5,7
  </code></pre>
  
  <p>How can I do this in Java? I'm trying to use recursion and I've looked at a <a href="https://stackoverflow.com/questions/1313642/simulating-nested-loops">similar question</a> already but I'm still coming up short. Thanks for the help! (P.S. this is not for a homework assignment)</p>