Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>There is no type-safe way to create an array of a parameterized type</strong> such as <code>Iterator&lt;T&gt;[]</code>.</p> <p>Alternatively, you can create a raw array: <code>Iterator&lt;?&gt;[]</code>. Or, if you can avoid the use of arrays entirely, use a collection type like <code>List&lt;Iterator&lt;T&gt;&gt;</code>.</p> <p>The reason it is not possible is that Java arrays are covariant and the parameterized bounds of Generic types are invariant. That is to say:</p> <pre><code>Integer[] integers = new Integer[1]; Number[] numbers = integers; // OK because Integer extends Number numbers[0] = new Double(3.14); // runtime exception </code></pre> <p>The compiler allows the assignment because <code>Double</code> extends <code>Number</code> and the declared type of <code>numbers</code> is <code>Number[]</code>. But at runtime the actual array object instance is the original <code>Integer[1]</code> and arrays know the type of the objects they contain.</p> <p>With generics, parameterized types are different. For one, due to <em>compile-time type erasure</em> they do not intrinsically know their runtime types.</p> <pre><code>List&lt;Integer&gt; integerList = new ArrayList&lt;Integer&gt;(); List&lt;Number&gt; numberList = integerList; // compiler error, prevents: numberList.add(new Double(3.14)); // would insert a Double into integerList Collection&lt;Integer&gt; integerCollection = integerList; // allowed // OK because List extends Collection and the &lt;type parameter&gt; did not change Collection&lt;Number&gt; numberCollection = integerList; // compiler error // an "Integer" is a "Number" // but "a collection of Integers" is more specific than "a collection of Numbers" // and cannot be generally treated the same way and guarantee correct behavior List&lt;?&gt; rawList = integerList; // allowed, but... rawList.add(new Integer(42)); // compiler error, Integer is not a ... a what? </code></pre> <p>With generics, in Java, you are relying on the compiler (not the runtime) to validate that the generic types are correct and safe.</p> <p>So while an <code>Iterator&lt;?&gt;[]</code> knows at runtime that it is an array that contains <code>Iterator</code> elements, the <code>&lt;T&gt;</code> in <code>Iterator&lt;T&gt;[]</code> is <em>erased</em> at compile time and the runtime has no way to know what it was supposed to be. So you get an unchecked warning.</p>
    singulars
    1. This table or related slice is empty.
    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