title: Write a method to compute all permutations of a string of unique characters
body: <p>When I test it with 5 elements set, I get the following exception:</p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 2</p>
  </blockquote>
  
  <p>Here is the code:</p>
  
  <pre><code>static ArrayList&lt;ArrayList&lt;Integer&gt;&gt; permutation(ArrayList&lt;Integer&gt; set){
  
      ArrayList&lt;ArrayList&lt;Integer&gt;&gt; permutations = new ArrayList&lt;ArrayList&lt;Integer&gt;&gt;();
  
      if (set.size() &lt;= 1) {
          permutations.add(set);
          return permutations;
      }
  
      for (int element : set){
          ArrayList&lt;Integer&gt; setcopy = new ArrayList&lt;Integer&gt;();  
          setcopy.addAll(set);
          setcopy.remove(element);
          add(permutations, element, permutation(setcopy));
      }       
      return permutations;
  }
  
  static void add(ArrayList&lt;ArrayList&lt;Integer&gt;&gt; permutations, int element, ArrayList&lt;ArrayList&lt;Integer&gt;&gt; setcopy){
  
  
      for (ArrayList&lt;Integer&gt; al : setcopy){
             ArrayList&lt;Integer&gt; p = new ArrayList&lt;Integer&gt;();
             p.add(element);
             p.addAll(al);
             permutations.add(p);
  
       }
  
  }
  </code></pre>