views:

183

answers:

4
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}"

Eclipse complains that there is a:

"Type mismatch: cannot convert from int to 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. 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.

A: 

Erm, ever thought about what would happen if you ran this code in a MaxHeap of, say, String objects?

T does not exist until you actually instantiate the generic class. So, it does not make sense to create an array of T with integers if you don't know what T is.

EDIT: Also, generics in Java only work with reference types, and int is a value type. Try using Integer (int's wrapper class) instead.

Etienne de Martel
I've already done that..i just thought i'd show the relevant parts. Should i include it in the question?
Kay
Oh. I see... I edited my answer.
Etienne de Martel
I still get an error:"The method Heapsort(T[], int) in the type MaxHeap<T> is not applicable for the arguments (Integer[], int)"when the Heapsort in main tries to use arr.
Kay
A: 

You've already said that T extends Comparable<T>. "int" does not extend Comparable<T> not matter how you cast it.

Paul Tomblin
What repercussion does this have? Is there no way i can implement my code?
Kay
+1  A: 

Declare arr as an Integer[] instead of a 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
}
Rob Heiser
Aggh! When an error disappears from one method it just pops up into another -- conservation of errors :(Error:"The method heapRebuild(T[], int, int) in the type MaxHeap<T> is not applicable for the arguments (T[], int, int)" in the method HeapsortThanks Rob
Kay
No need to convert `Heapsort` into a generic method - the class already is generic, so the method sees the `T` type parameter all right.
Péter Török
+1  A: 

When you use a generic type, you must resolve all the type parameters, i.e. tell the compiler which concrete types you want to use instead of the placeholder T in your code. 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. So you can rewrite your main method into something like

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. When you instantiate your class, you have to specify the type parameter Integer as above. 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.

Péter Török
Many many thanks Péter! That did the trick :)
Kay