title: Unable to generate sub-sets of an array
body: <p>I am trying to find the subsets of an array. In example if the array is [1,2] I am trying to print the following:</p>
  <pre><code>[1][2]
  [1]
  [2]
  null
  </code></pre>
  <p>The code that I have written is as follows:</p>
  <pre><code>    import java.util.*;
  
        class PrintArray {
             public static void printme(int a[], int pos, int size) {
                if (pos &gt;= size)
                 return;
             else
              System.out.println(a[pos]);
          pos++;
          printme(a, pos, size);
      }
  
      public static void generate(int a[], ArrayList&lt;Integer&gt; list, int pos, int size) {
  
          if (pos &gt; size) {
              for (int i = 0; i &lt; list.size() - 1; i++) {
                  System.out.print(&quot;[&quot; + list.get(i) + &quot;]&quot;);
              }
              System.out.println();
              return;
          }
  
          list.add(a[pos]);
          generate(a, list, pos + 1, size);
          list.remove(list.size() - 1);
          generate(a, list, pos + 1, size);
  
      }
  
      public static void main(String args[]) {
          int ar[] = { 1, 2, 3, 4, 5 };
          // printme(ar, 0, ar.length);
          ArrayList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();
          generate(ar, list, 0, ar.length);
  
      }
  }
  </code></pre>
  <p>However I am running into the following OOBE error:</p>
  <pre><code>Exception in thread &quot;main&quot; java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds 
  </code></pre>
  <p>That can be resolved by checking for pos&gt;=size instead of pos&gt;size but such a change does not generate all the sub-arrays.</p>
  <p>A follow-up problem is that I am getting duplicate outputs as shown below:</p>
  <pre><code>[2][3][4]
  [2][3]
  [2][3]
  [2]
  [2][4]
  [2]
  [2]
  </code></pre>
  <p>Could someone please help me in overcoming these 2 issues?</p>