title: Java Calculate all possible combinations of given int array
body: <p>I am trying to construct a program that would take an array of int({1,2,3} and a length value and calculate all possible combinations of this array.</p>
  
  <p>For example:</p>
  
  <pre><code>int[] arr= new char[] {0,1};
  int[] tes  = new int[3];
  possiblecomb(2, arr,tes,0);
  </code></pre>
  
  <p>This will output:</p>
  
  <pre><code> 00
   10
   01
   11
  </code></pre>
  
  <p>But i keep getting a Stack overflow error when i try to call the possiblecomb in the for loop</p>
  
  <pre><code> import java.util.Arrays;
  
   public class Program {
  
  public static void main(String[] args) {
  
      // Create an arr to work with
      int[] test = new int[] {0,1};
       int[] tes  = new int[3];
      // Find all possible combinations of this arr in the string size of 3
      possiblecomb(3, test,tes,0);
  }
  
  public static void possiblecomb(int maxLength, int[] nums, int[] curr,int end) {
  
      // If the current array has reached it's maximum length
      if(end == maxLength) {
          System.out.println(Arrays.toString(curr));
  
      // Else add each number from the numbs to new array and process these new arrays again
      } else {
          for(int i = 0; i &lt; nums.length; i++) {
              int[] oldCurr = curr.clone();
              curr[end]= nums[i];
              possiblecomb(maxLength,nums,curr,end++);
              curr = oldCurr.clone();
          }
      }
  }
  </code></pre>
  
  <p>}</p>