Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy isn't List.toArray() generic?
    primarykey
    data
    text
    <p>Why isn't List.toArray() generic? Why must you give the type as an argument (and usually create a new empty instance)?</p> <pre><code>public Object[] toArray() </code></pre> <p><a href="http://docs.oracle.com/javase/7/docs/api/java/util/List.html#toArray()" rel="nofollow noreferrer">http://docs.oracle.com/javase/7/docs/api/java/util/List.html#toArray()</a></p> <hr> <p>Update: I have since learned that generic array creation is not allowed, but I believe that it should be. Is there any reason why it's not allowed?</p> <pre><code>Main.java:28: error: generic array creation T[] ret = new T[size()]; </code></pre> <p><a href="http://ideone.com/3nX0cz" rel="nofollow noreferrer">http://ideone.com/3nX0cz</a></p> <hr> <p>Update: Ok I believe this is the answer:</p> <p><a href="http://docs.oracle.com/javase/tutorial/java/generics/restrictions.html#createArrays" rel="nofollow noreferrer">http://docs.oracle.com/javase/tutorial/java/generics/restrictions.html#createArrays</a></p> <p>Not because it is directly relevant, but indirectly relevant. There is nothing inherently wrong with <code>new T[size()]</code>, where <code>MyList&lt;String&gt;</code> would be turned into <code>new String[size()]</code>, but T could itself be a parameterized type. So if you were to create <code>MyList&lt;Set&lt;Integer&gt;&gt;</code>, then <code>T</code> would equal <code>Set&lt;Integer&gt;</code> and the compiler would try to create <code>new Set&lt;Integer&gt;[size()]</code>, which could lead to the problem in the link when returned. Someone tried to give an answer along these lines but that answer has since been deleted so I forgot who it was.</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.
 

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