Here we have generic method:
public static <T extends Comparable<T>> T[] function(T[] a)
{
Object[] m = new Object[2];
/* some work here */
return (T[]) m;
}
A ClassCastException
is thrown . What's wrong with it?
An array of Object is not an array of any subclass of Object. You are facing one of the limitations of generics in Java: you cannot create a generic array. See this thread for an approach that does work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With