OutOfMemory and NegativeArraySize exception
<p>i am using the following code in a program for finding permutation of array.But  it is showing the following exception.</p>

<pre><code>Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

Exception in thread "main" java.lang.NegativeArraySizeException
</code></pre>

<p>What should i do?plz suggest.</p>

<pre><code>public static String[] permute(String[] s)
{ //Returns a  list of permutations.
    if(s.length==1)
        return s;
    String[] array = new String[factorial(s.length)];
    ArrayList&lt;String&gt; x = new ArrayList&lt;String&gt;();
    for(int i=0; i&lt;s.length; i++){
        String[] y=permute(remove(s,i));
        for(String z:y){
            x.add(s[i]+z);
        }
    }
    return (String[])(x.toArray(array));
}
public static String[] remove(String[] s, int i){          
    String[] x = new String[s.length-1];
    ArrayList&lt;String&gt; arrayList =new ArrayList&lt;String&gt;();
    for(int j=0; j&lt;s.length; j++){
        if(j!=i)
            arrayList.add(s[j]);
    }
    return (String[])(arrayList.toArray(x));
}
</code></pre>
