[英]java.lang.ClassCastException using generics with mergeSort
I got the following exception in my code: 我的代码中出现以下异常:
java.lang.ClassCastException: [Ljava.lang.Comparable; java.lang.ClassCastException:[Ljava.lang.Comparable; cannot be cast to [Ljava.lang.Integer; 无法转换为[Ljava.lang.Integer; at the following method: 使用以下方法:
public static void comprobar() {
Integer a1[] = crearAleatorioInteger(100000);
Integer a2[] = Arrays.copyOf(a1, a1.length);
Ordenacion.quickSort(a1);
a2 = Ordenacion.mergeSort2(a2); //Here is where the exception is thrown
if(Ordenacion.sonIguales(a1,a2)) System.out.println("Bien");
else System.out.println("Mal");
}
I guess the mistake must be in the method mergeSort2()
which is in the class Ordenacion
, it is the following: 我猜这个错误一定是在Ordenacion
类中的mergeSort2()
方法中,它是以下内容:
public static <T extends Comparable<T>> T[] mergeSort2(T[] v) {
return mergeSort2(v, 0, v.length - 1);
}
private static <T extends Comparable<T>> T[] mergeSort2(T[] v, int i, int f){
if (f - i <= 1) {
T[] res = (T[]) new Comparable[f-i+1];
if(f==i) res[0] = v[i];
else if (v[i].compareTo(v[f]) > 0){
res[0] = v[f];
res[1] = v[i];
}
else{
res[0] = v[f];
res[1] = v[i];
}
return res;
}
else{
int m = (i + f) / 2;
return merge2(mergeSort2(v, i, m),mergeSort2(v, m + 1, f));
}
}
private static <T extends Comparable<T>> T[] merge2(T[] v1, T[] v2){
int a = 0, b = 0, k = 0;
T[] res = (T[]) new Comparable[v1.length+v2.length];
while(a < v1.length && b < v2.length){
if (v1[a].compareTo(v2[b]) < 0) res[k++] = v1[a++];
else res[k++] = v2[b++];
}
while (a < v1.length) res[k++] = v1[a++];
while (b < v2.length) res[k++] = v2[b++];
return res;
}
Thank you for any help you may offer 感谢您提供的任何帮助
You are creating the wrong array type and casting it to a "T[]" here... 您正在创建错误的数组类型,并将其转换为“ T []”。
T[] res = (T[]) new Comparable[f-i+1];
You need to create an array of type T which is not that straightforward. 您需要创建一个不是很简单的T类型的数组。
As there is no way to find out the actual type of a generic type variable at runtime, you might have to do something like this... 由于无法在运行时查找泛型类型变量的实际类型,因此您可能必须执行以下操作...
public static <T extends Comparable<T>> T[] mergeSort2(T[] v, Class<T> type) {
return mergeSort2(v, 0, v.length - 1, type);
}
private static <T extends Comparable<T>> T[] mergeSort2(T[] v, int i, int f, Class<T> type){
if (f - i <= 1) {
T[] res = (T[]) Array.newInstance(type, f-i+1);
...
UPDATE: 更新:
As @pbabcdefp pointed out, you don;t need to pass around the "Class" parameter. 正如@pbabcdefp指出的那样,您无需传递“ Class”参数。 You can use this instead... 您可以改用此...
T[] res = (T[]) Array.newInstance(v.getClass().getComponentType(), f-i+1);
This only works for arrays, if you had a List, you won't be able to get the type... 这仅适用于数组,如果您具有列表,则将无法获取类型...
You create an array of Comparable
here: 您在此处创建一个Comparable
数组:
T[] res = (T[]) new Comparable[f-i+1];
and try to cast it to Integer[]
. 并尝试将其转换为Integer[]
。 Which is obviously wrong, since Comparable
is no subclass of Integer
and thus the cast cannot be performed. 这显然是错误的,因为Comparable
不是Integer
子类,因此无法执行强制转换。 You'll have to create a new array of type T
. 您必须创建一个新的T
类型数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.