Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Generics and arrays don't mix, basically. The short answer is that you can work around this problem. The longer answer is that you probably shouldn't and I'll explain why.</p> <p>You could use <a href="http://download.oracle.com/javase/6/docs/api/java/lang/reflect/Array.html#newInstance(java.lang.Class,%20int...)" rel="nofollow noreferrer"><code>Array.newInstance()</code></a> like this:</p> <pre><code>private Comparable[] hashtable; ... hashtable = (Comparable[])Array.newInstance(Comparable.class, tableSize); </code></pre> <p>but you <strong>can't</strong> create an array of your parameterized type.</p> <p>Arrays are <em>covariant</em>. That means they retain the type of their elements at runtime. Java's generics are not. They use <em>type erasure</em> to basically mask the implicit casting that is going on. It's important to understand that.</p> <p>So when you create an Object array you can't cast it to, say, a Comparable array (or any other type) because that is not correct.</p> <p>To give you an example. With generics this is perfectly legal:</p> <pre><code>List&lt;String&gt; list = new ArrayList&lt;String&gt;(); List&lt;Integer&gt; list2 = (List&lt;Integer&gt;)list; list.add(3); </code></pre> <p>It's also why you can't do this:</p> <pre><code>public &lt;T&gt; T newInstance(T t) { return new T(); // error! } </code></pre> <p>ie at runtime there is no knowledge of T's class. This is why the above code is more often written as:</p> <pre><code>public &lt;T&gt; T newInstance(T t, Class&lt;T&gt; clazz) { return clazz.newInstance(); } </code></pre> <p>because their is no runtime type for the generic argument. But with arrays:</p> <pre><code>String arr[] = new String[10]; Integer arr2[] = (Integer[])arr; // error! </code></pre> <p>What you should be doing in this case (imho) is not using arrays but using an <code>ArrayList</code>. In all honesty, there is very little reason to use arrays over an <code>ArrayList</code> and generics is just one example of that.</p> <p>For a better and more complete explanation see the (excellent) <a href="http://www.angelikalanger.com/GenericsFAQ/FAQSections/ParameterizedTypes.html#FAQ104" rel="nofollow noreferrer">Java Generics FAQ</a>:</p> <blockquote> <h2>Can I create an array whose component type is a concrete parameterized type?</h2> <p><strong><em>No, because it is not type-safe.</em></strong></p> <p>Arrays are covariant, which means that an array of supertype references is a supertype of an array of subtype references. That is, <code>Object[]</code> is a supertype of <code>String[]</code> and a string array can be accessed through a reference variable of type <code>Object[]</code>. </p> <p>...</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload