Permutation - Need help desining a better block that won't need unused values
<p>I need help making my permutation's (order important, repeatable) size smaller by discarding the unneeded permutations before hand.</p>

<ul>
<li><p>The current permutation takes a global minimum and maximum from the values provided. However, some of the permutations are discarded afterward as they don't fall within the range needed.</p></li>
<li><p>The idea is there are for example 3 numbers which need a permutation. For example: 1-3, 8-10 and 5-15. The current code will create a permutation of 1-15 even though values from 4-7 will be discarded later on.</p></li>
</ul>

<p>Unfortunately in some instances its not possible to create an array large enough in Java to contain the permutation results. </p>

<p>Any help would be appreciated on changing this permutation to only include necessary values prior to computing the permutation. </p>

<p><strong>PermutationCore:</strong> </p>

<pre><code>public class PermutationCore {
    private String[] a;
    private int n;

    public PermutationCore(String[] arrayOfPossibilities, int lengthOfPermutation) {
        this.a = arrayOfPossibilities;
        this.n = lengthOfPermutation;
    }

    public String[][] getVariations() {
        int l = a.length;
        int permutations = (int) Math.pow(l, n);

        Co.println("Permutation array size: " + permutations);

        String[][] table = new String[permutations][n];

        for (int x = 0; x &lt; n; x++) {
            int t2 = (int) Math.pow(l, x);
            for (int p1 = 0; p1 &lt; permutations;) {
                for (int al = 0; al &lt; l; al++) {
                    for (int p2 = 0; p2 &lt; t2; p2++) {
                        table[p1][x] = a[al];
                        p1++;
                    }
                }
            }
        }

        return table;
    }
}
</code></pre>

<p><strong>Permutation</strong></p>

<pre><code>public class Permutation {
    private ArrayList&lt;Iteration&gt; listOfIteration = new ArrayList&lt;Iteration&gt;();
    private boolean prepared;
    private PermutationCore permutationCore;
    private int min = Integer.MAX_VALUE;
    private int max = Integer.MIN_VALUE;
    private int count = 0;
    private String[][] arrayOfStringResults;

    public void addIteration(Iteration iteration){
        if (prepared){throw new IllegalStateException("Permuation is already prepared. Create a new instance to add new items");}
        this.listOfIteration.add(iteration);
    }

    public void prepare(){
        String[] arrayOfString;

        for (Iteration iteration : listOfIteration){
            if (iteration.end &gt; max){max = iteration.end;}
            if (iteration.start &lt; min){min = iteration.start;}
        }

        arrayOfString = new String[max-min+1];

        for (int i=0; i&lt;arrayOfString.length; i++){
            arrayOfString[i] = String.valueOf(min+i);
        }

        permutationCore = new PermutationCore(arrayOfString, listOfIteration.size());

        prepared = true;

//      Co.println("Min/max: " + min + "," + max);

        arrayOfStringResults = permutationCore.getVariations();
//      ArrayTools.sort2DStringArray(arrayOfStringResults);

    }

    public boolean iterate(){
        LABEL_ITERATE_LOOP: {
            int i=0;

            if (count == arrayOfStringResults.length){
                return false;
            }

            for (Iteration iteration : listOfIteration){
                int currentValue = Integer.valueOf(arrayOfStringResults[count][i]);

                if (currentValue &gt; iteration.end || currentValue &lt; iteration.start){
                    //Co.println("Failed at: " + iteration.start + "," + iteration.end + " / " + currentValue);
                    count++;
                    break LABEL_ITERATE_LOOP;
                }

                iteration.current = currentValue;
                i++;
            }

            count++;
        }

        return true;
    }

    public Iteration getIteration(Object request) {
        for (Iteration iteration : listOfIteration){
            if (iteration.request == request){
                return iteration;
            }
        }

        return null;
    }

    public ArrayList&lt;Iteration&gt; getListOfIterations(){
        return listOfIteration;
    }

    public static class Iteration{
        private int start;
        private int end;
        private int current;
        private Object request;

        public Iteration(int start, int end, Object request){
            this.start = start;
            this.end = end;
            this.request = request;
        }

        public double getCurrentValue(){
            return this.current;
        }

        public Object getRequest(){
            return this.request;
        }
    }
}
</code></pre>
