title: Generic method to convert an array to ArrayList
body: <p>I have a method to convert an array to an ArrayList as follows:</p>
  
  <pre><code>    public static &lt;T&gt; ArrayList&lt;T&gt; getArrayList(T[] a){
          ArrayList&lt;T&gt; retList = new ArrayList&lt;T&gt;();
          for (T i : a){ 
              retList.add(i); 
          }
          return retList;
      }
  </code></pre>
  
  <p>which works fine for object arrays such as:</p>
  
  <pre><code>    String[] arr = {"String","'nother string","etc"};
      ArrayList&lt;String&gt; stringList = initArrayList(arr);
  </code></pre>
  
  <p>But not with primitive arrays:</p>
  
  <pre><code>    int[] arr2 = {1,2,3};
      ArrayList&lt;Integer&gt; intList = initArrayList(arr2); //Compiler is insulted by this.
  </code></pre>
  
  <p>I guess I have to convert the array to an Integer array if I want the method to work, but is there a way to make the method a little smarter about how it handle's this?</p>
  
  <p>The Java tutorials site has the following:</p>
  
  <pre><code>    static &lt;T&gt; void fromArrayToCollection(T[] a, Collection&lt;T&gt; c) {
          for (T o : a) {
              c.add(o); // Correct
          }
      }
  </code></pre>
  
  <p>Which would work, but I'd like the method to be creating the ArrayList.</p>
  
  <p>Also, this is just for my own amusement, so the type of Collection doesn't really matter, I just used ArrayList when I wrote the method.</p>
  
  <p>Thanks</p>