title: How to instantiate an ArrayList<?> and add an item through reflection with Java?
body: <p>I am writing a deserialization method that converts xml to a Java object.  I would like to do this dynamically and avoid writing hard coded references to specific types.</p>
  
  <p>For example this is a simplified version of one of my classes.</p>
  
  <pre><code>public class MyObject { 
     public ArrayList&lt;SubObject&gt; SubObjects = new ArrayList&lt;SubObject&gt;();
  }
  </code></pre>
  
  <p>Here is a stripped down version of the method:</p>
  
  <pre><code>public class Serializer {    
      public static &lt;T&gt; T fromXml(String xml, Class&lt;T&gt; c) {
         T obj = c.newInstance();       
         Field field = obj.getClass().getField("SubObjects");    
         //help : create instance of ArrayList&lt;SubObject&gt; and add an item
         //help#2 : field.set(obj, newArrayList);
  
         return obj;
      }
  }
  </code></pre>
  
  <p>Calling this method would look like this:</p>
  
  <pre><code>MyObject obj = Serializer.fromXml("myxmldata", MyObject.class);
  </code></pre>
  
  <p>Forgive me if this is a trivial problem as I am a C# developer learning Java.</p>
  
  <p>Thanks!</p>