Java Errors: method cannot be applied to given types, inference variable has incompatible bounds

59 views Asked by At

While working on implementing the Merge Sort method in Java in a Sorting.java file, as part of an assignment, I am attempting to test my code with a separate Driver.java file.

Here is the header of the mergeSort() method in the Sorting.java file:

public class Sorting {
    public static <T> void mergeSort(T[] arr, Comparator<T> comparator) {
    }
}

Then, here is how I am trying to test it in the Driver.java file:

public class Driver {
    public static void main(String[] args) {
        int[] testMergeSort1 = { 9, 13, 5, 6, 12, 10, 3, 7, 2, 8, 0, 15, 1, 4, 14, 11 };
        Sorting.mergeSort(testMergeSort1, new IntegerComparator());
        System.out.println(Arrays.toString(testMergeSort1));
    }

    public class IntegerComparator implements Comparator<Integer> {
        @Override
        public int compare(Integer a, Integer b) {
            return a.intValue() - b.intValue();
        }
    }
}

Here are the errors that compiling the file generates:

Driver.java:29: error: non-static variable this cannot be referenced from a static context
        Sorting.mergeSort(testMergeSort1, new IntegerComparator());
                                          ^
Driver.java:29: error: method mergeSort in class Sorting cannot be applied to given types;
        Sorting.mergeSort(testMergeSort1, new IntegerComparator());
               ^
  required: T[],Comparator<T>
  found:    int[],Driver.IntegerComparator
  reason: inference variable T has incompatible bounds
    equality constraints: Integer,int
    lower bounds: Object
  where T is a type-variable:
    T extends Object declared in method <T>mergeSort(T[],Comparator<T>)

Trying Sorting.mergeSort(testMergeSort1, new IntegerComparator); instead of Sorting.mergeSort(testMergeSort1, new IntegerComparator()); generates the following error:

error: '(' or '[' expected
        Sorting.mergeSort(testMergeSort1, new IntegerComparator;

Any guidance understanding what the issue is and how to move towards resolving it would be appreciated.

0

There are 0 answers