Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's:</p> <pre><code>add(i.toArray(new Animal[i.size()])) </code></pre> <p><a href="http://download.oracle.com/javase/6/docs/api/java/util/List.html#toArray%28%29">List.toArray</a> returns an <code>Object[]</code>, regardless of the type argument on the List: even though you might write <code>new List&lt;String&gt;().toArray()</code>, you will get an <code>Object[]</code>. However, the <a href="http://download.oracle.com/javase/6/docs/api/java/util/List.html#toArray%28T%5B%5D%29">version of toArray that takes an array to fill</a> returns an array with the correct type: if you write <code>new List&lt;String&gt;().toArray(new String[0])</code>, you will get an <code>String[]</code>. Note that the size of the array you pass in doesn't even have to match the size of the list, although it's good practice to ensure that it does.</p> <p>This is ultimately due to a mildly tricky feature of generics. At first glance, you might think that <code>String[]</code> and <code>List&lt;String&gt;</code>mean similar things for their base types - one is an array of strings, the other is a list of strings.</p> <p>However, they are in fact very different.</p> <p>An array is a language primitive, and has its type baked into it. If you took a hex editor and looked at an array instance in memory in the JVM, you would be able to find (somewhere nearby) a record of the type of objects it holds. That means that if you take an instance of an array of some unknown component type, you can <a href="http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#getComponentType%28%29">find out what that type is</a>. Conversely, it means that if you're going to <a href="http://download.oracle.com/javase/6/docs/api/java/lang/reflect/Array.html#newInstance%28java.lang.Class,%20int%29">create an instance of an array</a>, you need to know what component type you want.</p> <p>The <code>List</code>, on the other hand, uses generics, which in Java is implemented with <a href="http://download.oracle.com/javase/tutorial/java/generics/erasure.html">type erasure</a>, which means that, roughly speaking, it is something that exists in the compiler, but not at runtime (the compiler can check that you get it right, but the JVM can't). This leads to a simple and efficient implementation (one simple enough to have been added to pre-generics Java without changing the JVM), but it has some shortcomings - in particular, that at runtime, there is no way to tell what the type argument on any particular instance of a generic class is, because type arguments only exist in the compiler. Because it is up to the <code>List</code> instance to handle <code>toArray()</code>, the only thing it can do is create an <code>Object[]</code>. It just doesn't know of a more specific type to use.</p> <p>One way of looking at this is that arrays have a type argument as part of their <em>class</em>, whereas <code>List</code>s have a type argument as part of their <em>type</em>, and since objects have classes but variables have types, you can't get the type argument of a <code>List</code> from an object, only from a variable holding an object (as an aside, you also can't get the type argument of an array from a variable holding an array (consider <code>Object[] array = new String[0];</code>), but that doesn't really matter because, the variable lets you get hold of an object - unless it's null).</p> <p>To boil this down to code, the problem is:</p> <pre><code>public &lt;E&gt; E[] createSimilarlyTypedArray(List&lt;E&gt; list) { Class&lt;E&gt; componentType = list.???; // there is no way to do this return Arrays.newInstance(componentType, list.size()); } </code></pre>
    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