title: Java - Permutation of ArrayList elements (Integer) - Can't get it to work properly
body: <p>I've been looking around quite a bit to solve my issue. I got many problems solved but this one is still haunting me :S It's been a long time I haven't touch Java programming (programming in general) so be understanding out there! ;)</p>
  
  <p>My goal is to get all the combination possible out of an array of integers. When I use the following code, applied to the test array of integer {1, 2, 3, 4}, I expect to have:
  <br>1 2 3 4
  <br>1 2 4 3
  <br>1 3 2 4
  <br>1 3 4 2
  <br>2 1 3 4
  <br>2 1 4 3
  <br>(...)
  <br>but here is what I get
  <br>1 2 3 4
  <br>1 2 3 4 4 3
  <br>1 2 3 4 4 3 3 2 4</p>
  
  <p>Does anybody have a clue, a suggestion or even a solution? Thanks in advance!</p>
  
  <pre><code>public class Calculation{
  (...)
    public void Permute(ArrayList&lt;Integer&gt; soFar,ArrayList&lt;Integer&gt; rest){
      if(rest.isEmpty())    this.fillMatrice(convertIntegers(soFar)); // there it goes in a previously created arrow of int
      else{
          for(int k=0;k&lt;rest.size();k++){
              ArrayList&lt;Integer&gt; next=new ArrayList&lt;Integer&gt;();
              next=soFar;
              next.add(rest.get(k));
              ArrayList&lt;Integer&gt; remaining=new ArrayList&lt;Integer&gt;();
              List&lt;Integer&gt; sublist = rest.subList(0, k);
              for(int a=0;a&lt;sublist.size();a++)   remaining.add(sublist.get(a));
              sublist = rest.subList(k+1,rest.size());
              for(int a=0;a&lt;sublist.size();a++)   remaining.add(sublist.get(a));
              Permute(next,remaining);
          }
      }
  }
  public static ArrayList&lt;Integer&gt; convertArray(int[] integers){
      ArrayList&lt;Integer&gt; convArray=new ArrayList&lt;Integer&gt;();
      for(int i=0;i&lt;integers.length;i++)  convArray.add(integers[i]);
      return convArray;
  }
  public static int[] convertIntegers(List&lt;Integer&gt; integers){
      int[] ret = new int[integers.size()];
      for(int i=0;i&lt;ret.length;i++)   ret[i]=integers.get(i).intValue();
      return ret;
  }
  public Calculation() {
      (...)
      ArrayList&lt;Integer&gt; soFar=new ArrayList&lt;Integer&gt;();
      int[] test={1,2,3,4};
      Permute(soFar,convertArray(test));
  }
  </code></pre>