title: The add(elem) method changes every element in my array to elem?
body: <p>I have the following code in my class:</p>
  
  <pre><code>public static ArrayList&lt;String[]&gt; allCombos;
  
  public static void main(String[] args){
      allCombos = new ArrayList&lt;String[]&gt;();
      String[] arr = {"A","B","C","D","E","F"};
      combinations(arr, 3, 0, new String[3]);
  
  }
  
  static void combinations(String[] arr, int len, int startPosition, String[] result){
      if (len == 0){
          allCombos.add(result);   // this is where the problem seems to be
          return;
      }      
      for (int i = startPosition; i &lt;= arr.length-len; i++){
          result[result.length - len] = arr[i];
          combinations(arr, len-1, i+1, result);
      }
  }       
  </code></pre>
  
  <p>For some reason, each time <code>allCombos.add(result)</code> is called in <code>combinations()</code>, the method seems to set every single element in the whole array to the current value of result, thereby over-riding what the previous iteration of <code>combinations()</code> had set the most recently added value of <code>allCombos</code> as. The same thing happens if <code>allCombos</code> is a <code>Vector</code>.</p>
  
  <p>Can anybody tell me why this is happening and how I can fix it? </p>