How to test class for element type in an empty ArrayList?
<p>So I have a generic ArrayList passed from parameter.</p>

<p>For example:</p>

<pre><code>private &lt;T&gt; void testClass(ArrayList&lt;T&gt; data){
  if(data[0] instanceof Foo){
    //do something
  } eles if(data[0] instanceof Bar){
    //do something else
  }
}
</code></pre>

<p>But the arraylist does not guarantee to have an element inside and could be empty, and <code>get(0)</code> will raise exception. How can I do this?</p>

<hr>

<p>Edit:</p>

<p>How about non-initiated generic array?</p>

<p>For example:</p>

<pre><code>private &lt;T&gt; void testClass(T[] data){
  if(data.get(0) instanceof Foo){
    //do something
  } eles if(data.get(0) instanceof Bar){
    //do something else
  }
}
</code></pre>

<p>I tried <code>T.class.getName().equals("Foo")</code> but it does not work...</p>
