[英]Java Generic Casting Type Mismatch
public class MaxHeap<T extends Comparable<T>> implements Heap<T>{
private T[] heap;
private int lastIndex;
public void main(String[] args){
int i;
T[] arr = {1,3,4,5,2}; //ERROR HERE *******
foo
}
public T[] Heapsort(T[]anArray, int n){
// build initial heap
T[]sortedArray = anArray;
for (int i = n-1; i< 0; i--){
//assert: the tree rooted at index is a semiheap
heapRebuild(anArray, i, n);
//assert: the tree rooted at index is a heap
}
//sort the heap array
int last = n-1;
//invariant: Array[0..last] is a heap,
//Array[last+1..n-1] is sorted
for (int j=1; j<n-1;j++) {
sortedArray[0]=sortedArray[last];
last--;
heapRebuild(anArray, 0, last);
}
return sortedArray;
}
protected void heapRebuild(T[ ] items, int root, int size){
foo
}
}
The error is on the line with " T[arr] = {1,3,4,5,2}
" 错误出现在“
T[arr] = {1,3,4,5,2}
”行中
Eclipse complains that there is a: Eclipse抱怨有一个:
"Type mismatch: cannot convert from int to T"
“类型不匹配:无法从int转换为T”
I've tried to casting nearly everywhere but to no avail.A simple way out would be to not use generics but instead just ints but that's sadly not an option. 我试图在几乎所有地方进行转换,但都无济于事,一个简单的方法是不使用泛型,而只使用int,但遗憾的是这不是一种选择。 I've got to find a way to resolve the array of ints
{1,3,4,5,2}
into an array of T so that the rest of my code will work smoothly. 我必须找到一种方法来将整数
{1,3,4,5,2}
的数组解析为T的数组,以便我的其余代码能够顺利运行。
Declare arr
as an Integer[]
instead of a T[]
. 将
arr
声明为Integer[]
而不是T[]
。 There are also a couple of other small errors that I fixed here: 我在这里还修复了其他一些小错误:
public static void main(String[] args){
int i;
Integer[] arr = {1,3,4,5,2}; //ERROR HERE *******
}
public <T> T[] Heapsort(T[]anArray, int n){
// build initial heap
T[]sortedArray = anArray;
for (int i = n-1; i< 0; i--){
//assert: the tree rooted at index is a semiheap
heapRebuild(anArray, i, n);
//assert: the tree rooted at index is a heap
}
//sort the heap array
int last = n-1;
//invariant: Array[0..last] is a heap,
//Array[last+1..n-1] is sorted
for (int j=1; j<n-1;j++) {
sortedArray[0]=sortedArray[last];
last--;
heapRebuild(anArray, 0, last);
}
return sortedArray;
}
protected void heapRebuild(T[ ] items, int root, int size){
//foo
}
When you use a generic type, you must resolve all the type parameters, ie tell the compiler which concrete types you want to use instead of the placeholder T
in your code. 使用泛型类型时,必须解析所有类型参数,即告诉编译器要使用哪些具体类型而不是代码中的占位符
T
As the others already pointed out, a primitive type like int
can't be used as a generic type parameter - it must be a reference type, like Integer
. 正如其他人已经指出的那样,像
int
这样的原始类型不能用作泛型类型参数-它必须是引用类型,例如Integer
。 So you can rewrite your main
method into something like 因此,您可以将
main
方法重写为类似
public static void main(String[] args){
int i = 5;
Integer[] arr = {1,3,4,5,2};
MaxHeap<Integer> maxHeap = new MaxHeap<Integer>();
maxHeap.heapSort(arr, i);
}
Note that it should be static
. 注意它应该是
static
。 When you instantiate your class, you have to specify the type parameter Integer
as above. 实例化类时,必须如上所述指定类型参数
Integer
。 Then you can pass it the array to be sorted. 然后,您可以将要传递的数组传递给它。
A further note: this loop 进一步说明:此循环
for (int i = n-1; i< 0; i--){
...
}
will never execute - the loop condition should be i > 0
instead. 永远不会执行-循环条件应改为
i > 0
。
Erm, ever thought about what would happen if you ran this code in a MaxHeap of, say, String objects? 嗯,有没有想过如果在MaxHeap(例如String对象)中运行此代码会发生什么情况?
T does not exist until you actually instantiate the generic class. 在实际实例化泛型类之前,T不存在。 So, it does not make sense to create an array of T with integers if you don't know what T is.
因此,如果您不知道T是什么,那么用整数创建T的数组是没有意义的。
EDIT: Also, generics in Java only work with reference types, and int is a value type. 编辑:另外,Java中的泛型仅适用于引用类型,而int是值类型。 Try using Integer (int's wrapper class) instead.
尝试改为使用Integer(int的包装器类)。
You've already said that T extends Comparable<T>
. 您已经说过
T extends Comparable<T>
。 "int" does not extend Comparable<T>
not matter how you cast it. 无论如何转换,“ int”都不会扩展
Comparable<T>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.