Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
What I wrote is a class where I am trying to copy an anytype array and check if they are the same. However, when I try to run it, I get
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
at chapter4.exer20.copy(exer20.java:9).
package chapter4;
public class exer20{
public static void main(String [] args){
String[] ar = {"17", "13", "213", "52", "65", "7"};
System.out.println(check(ar, copy(ar)));
}
@SuppressWarnings("unchecked")
public static <AnyType extends Comparable<? super AnyType>> AnyType[] copy(AnyType[] a){
AnyType [] ar = (AnyType[]) new Object[a.length];
for(int i = 0; i<a.length;i++)
ar[i] = a[i];
return ar;
}
public static <AnyType extends Comparable<? super AnyType>> boolean check(AnyType[] a, AnyType[] b){
if(a.length!=b.length) return false;
for(int i = 0; i<a.length;i++){
if(!a[i].equals(b[i])) return false;
}
return true;
}
}Can anyone tell me what is wrong with this?