-1

This is heapifying the array question and this is my constructor of the heapcls which extends to Comparable

heapcls(T[] arr,boolean flag){   

        this(flag);
        for (T item : arr) {
            this.data.add(item);
        }
        for(int i=this.data.size()-1;i>=0;i--){
            this.downheapify(i);
        }
    }

public static void main(String[] args) {

        int[]arr={7,8,9,5,11,3,10,1,6,2,4,12,0,-1,13};
        heapcls<Integer> client=`enter code here`new heapcls<>(arr,false);
}
  this statement is giving ," Cannot infer type arguments error"      

how should I change my CompareTo(by default function) to rectify the error. since i havent overridden the bydefault CompareTo function.pls guide me.

Rajith Pemabandu
  • 616
  • 7
  • 14
tanishq
  • 9
  • 1
  • By the way, there's no need to call `downheapify` on every element of the array: just the first half. You can speed up your code by changing your `for` statement initialization to `this.data.size()/2`. – Jim Mischel Jul 18 '17 at 12:42

1 Answers1

1

The actual issue is that you're trying to pass an int[] to a constructor which has T[] as a parameter. Since the type on the left-hand side of the assignment is heapcls<Integer>, the constructor is supposed to have Integer[] passed to it as an argument.

Type arguments to generic types can't be primitive types and arrays of primitives aren't autoboxed. (Autoboxing an array would require allocating an entire new array.)

It seems like you could fix this just by using Integer[] instead of int[].

I'm not really sure why we get the "cannot infer type arguments" message in cases like this. I think it's a bug with the Java compiler when using the diamond type (i.e. <>) and the arguments to the constructor aren't applicable to any of the declared constructors. (Here's a much more minimal example showing the same issue with the error message: http://ideone.com/wJx16i.)

In any case, your code shouldn't compile, but because you're trying to pass an int[] as an argument to a constructor which effectively has Integer[] as a parameter, not really because the type arguments can't be inferred.

As a side note, class names in Java start with an upper-case character by convention, so your heapcls should be HeapCls (and probably just something like Heap).

Radiodef
  • 37,180
  • 14
  • 90
  • 125