0
public class MergeSort
{


public static void sort(Comparable[] a)
{
    Comparable[] aux = new Comparable[a.length];
    
    sort(a, aux, 0, a.length);
}

private static void sort(Comparable[] a, Comparable[] aux, int lo, int hi)
{
    
    if (hi - lo <= 1) return;
    int mid = lo + (hi+lo)/2;
    
    sort(a, aux, lo, hi);
    sort(a, aux, mid, hi);

    int i = lo, j = mid;

    for (int k = lo; k < hi; k++)
    {
        if (i == mid) aux[k] = a[j++];
        else if (j == hi) aux[k] = a[i++];
        else if (a[j].compareTo(a[i]) > 0) aux[k] = a[j++];
        else aux[k] = a[i++];           
    }
    for (int k = lo; k < hi; k++)
    {
        a[k] = aux[k];
    }   

}


public static void main(String[] args)
{
    //Read strings from standard input
    String[] a = StdIn.readAllStrings();
    
    sort(a);

    for (int i = 0; i < a.length; i++)
    {
        StdOut.print(a[i] + " ");
    }
    StdOut.println();
}

}

I get this message when trying to compile: MergeSort.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.

When I compile with -Xlint I get these errors:

MergeSort.java:7: warning: [rawtypes] found raw type: Comparable
        public static void sort(Comparable[] a)
                                ^
  missing type arguments for generic class Comparable<T>
  where T is a type-variable:
    T extends Object declared in interface Comparable

MergeSort.java:9: warning: [rawtypes] found raw type: Comparable
                Comparable[] aux = new Comparable[a.length];
                ^
  missing type arguments for generic class Comparable<T>
  where T is a type-variable:
    T extends Object declared in interface Comparable

MergeSort.java:9: warning: [rawtypes] found raw type: Comparable
                Comparable[] aux = new Comparable[a.length];
                                       ^
  missing type arguments for generic class Comparable<T>
  where T is a type-variable:
    T extends Object declared in interface Comparable

MergeSort.java:14: warning: [rawtypes] found raw type: Comparable
        private static void sort(Comparable[] a, Comparable[] aux, int lo, int hi)
                                 ^
  missing type arguments for generic class Comparable<T>
  where T is a type-variable:
    T extends Object declared in interface Comparable

MergeSort.java:14: warning: [rawtypes] found raw type: Comparable
        private static void sort(Comparable[] a, Comparable[] aux, int lo, int hi)
                                                 ^
  missing type arguments for generic class Comparable<T>
  where T is a type-variable:
    T extends Object declared in interface Comparable

MergeSort.java:29: warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type Comparable
                        else if (a[j].compareTo(a[i]) > 0) aux[k] = a[j++];
                                               ^
  where T is a type-variable:
    T extends Object declared in interface Comparable
6 warnings
djm.im
  • 3,295
  • 4
  • 30
  • 45
BadMojo
  • 1
  • 2
  • 2
    `Comparable` is a generic type so you'd need to add a type. It would be easier with instance methods but try `public static > void sort(T[] a)` etc. `Comparable[] aux = new Comparable[a.length];` would be a problem since you can't simply create a `T[]` but there are ways around this (e.g. this: https://www.baeldung.com/java-generic-array and this: https://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java). – Thomas Nov 04 '21 at 16:49
  • Thanks! Will try these solutions! – BadMojo Nov 04 '21 at 17:43

0 Answers0