public class ArrayHeap<T extends Comparable<T>> implements Heap<T>{
...
public ArrayHeap(int capacity){
heap = (T[]) new Comparable[capacity];
}
Hi there, just have a quick question on the line heap = ... So in class, we have been discussing heaps and implementing them using arrays (using generic types). Just doing some review and can't get my head around what's actually happening here on line 5.
So we cannot create objects of the generic type, normally I would expect to see:
heap = (T[]) new Object[capacity]
I.e. If I was to read this and what it says: Set heap to the result of casting a new Object to a generic array of type T, and set the size to capacity.
My question is, how can the line:
heap = (T[]) new Comparable[capacity];
be read? Is it creating a new Comparable object or a new Object that implements the comparable interface?