Generics question: ArrayList<T>.toArray() throwing a ClassCastException

In the following code

 list = new ArrayList();
      Integer[] array = list.toArray();
   }
}
]]>


Why is the toArray() throwing a CCE?

Jacques

5 comments
Comment actions Permalink

Just because toArray() with no parameters returns actually Object[], not
Integer[].
There is a second method public T[] toArray(T[] ts) that does what you need. The reason for this design lays in the fact that for the first method the compiler cannot generate method that returns Integer[] since no type information is preserved at runtime, thus you must provide an instance of correct type, and all that compiler can guarantee is that the return type will be the same. Friendly, Eugene "Jacques Morel" ]]> wrote in message
news:33549157.1064124480847.JavaMail.itn@is.intellij.net...

In the following code

 import java.util.ArrayList;
>
> public class ToArray {
>    public static void main(String[] args) {
>       ArrayList list = new ArrayList();
>       Integer[] array = list.toArray();
>    }
> }
> ]]>

>

Why is the toArray() throwing a CCE?

>

Jacques



0
Comment actions Permalink

So now I am completely confused:
If I change the toArray line to

I get a compilation error (and IDEA editor parser does not complain):


Now if I use the old, non-generic syntax, with a explicit cast it works ok (and IDEA editor complains that the cast is not needed!)


Am I doing something wrong here?

0
Comment actions Permalink

Jacques Morel wrote:

So now I am completely confused:
If I change the toArray line to

 Integer[] array = list.toArray(new Integer[0]);
> ]]>

I get a compilation error (and IDEA editor parser does not complain):
incompatible types > found: java.lang.Integer[-] > required: java.lang.Integer[] > ]]>


Seems like you're using generics preview 2.0, with variance, where the
method returns an array of type java.lang.Integer[-].

IDEA does not support variance, which is why the parser doesn't complain
(and why it does complain about the explicit cast you added). Variance
was unfortunately removed from generics 2.2, so things will change when
you upgrade...

0
Comment actions Permalink

On a more general note, generics implementation of arrays is a total mess.
AFAIU, in fact you cannot have an array of type parameters almost anywhere
in your code.

Friendly,
Dmitry

Jacques Morel wrote:

In the following code

 import java.util.ArrayList;
> 
> public class ToArray {
>    public static void main(String[] args) {
>       ArrayList list = new ArrayList();
>       Integer[] array = list.toArray();
>    }
> }
> ]]>


Why is the toArray() throwing a CCE?

Jacques


--
Dmitry Lomov
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0
Comment actions Permalink

That's exactly why variance was attempted. Typesafe generic arrays was the main problem variance tried to address in the 2.2 prototype. It's a shame it was dropped after that :(

0

Please sign in to leave a comment.