Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - Permutation of ArrayList elements (Integer) - Can't get it to work properly
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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