title: Why doesn't my code work for outputting of all subsets of a set work?
body: <p>I'm trying to write code to output all subsets of a set (represented by a list of integers).
  Why doesn't the following code work?</p>
  
  <pre class="lang-java prettyprint-override"><code>public static List&lt;List&lt;Integer&gt;&gt; powerSet(List&lt;Integer&gt; l){
      List&lt;List&lt;Integer&gt;&gt; result = new ArrayList&lt;List&lt;Integer&gt;&gt;();
      if (l.size() == 0) {
          result.add(new ArrayList&lt;&gt;());
      }
      else { 
          List&lt;List&lt;Integer&gt;&gt; prev = powerSet(l.subList(0, l.size() - 1));
          for (List&lt;Integer&gt; x : prev) {
              x.add(l.get(l.size() - 1));
              result.add(x);
          }
      }
      return result;
  }
  </code></pre>
  
  <p>My thinking is that to compute the subsets of a set A[0...n-1] of size n, we can just add the last element A[n - 1] of the set to the subsets of A[0...n-2].
  However, the code above only gives me the same set:</p>
  
  <pre class="lang-java prettyprint-override"><code>public static void main(String[] args) {
  
      List&lt;Integer&gt; l = new ArrayList&lt;&gt;();
      l.add(0);
      l.add(1);
      l.add(2);
  
      List&lt;List&lt;Integer&gt;&gt; result = powerSet(l);
  
      System.out.println(result.toString());
  }
  </code></pre>