1

So, I have got a problem in my Main class when I would like to call the mergeSort() method caused by the Comparator. I get the following message:

Image of the error message

I have no idea how to fix that issue.. please help me!

Notice: Don't wonder that there doesn't happen anything in the code. I got stuck because I cannot prove the functionality of my code because of the above described problem :(

(Sry for my bad english)

class Algorithms
{
    public static <T> void mergeSort(final T[] a, final Comparator<T> c)
    {
        T[] list = a;
        Comparator<T> comp = c;
    }
}


public class Main
{
    public static void main(String[] args)
    {
        int[] unsortedList = {4,5,7,1,98,32}; //Expected = 1,4,5,7,32,98

        Comparator<Integer> sorted = Comparator.naturalOrder();
        int[] sortedList = Algorithms.mergeSort(unsortedList,sorted))
    }
}
2
  • Also doesn't help that you're expecting a return value from a mergeSort, which is a void method. Commented May 29, 2016 at 14:42
  • Yeah, you are right! I think that happened while trying many things to get that issue worked... :D Commented May 29, 2016 at 15:00

4 Answers 4

3

In this code, the types don't match in the Algorithms.mergeSort call:

int[] unsortedList = {4,5,7,1,98,32}; //Expected = 1,4,5,7,32,98

Comparator<Integer> sorted = Comparator.naturalOrder();
Algorithms.mergeSort(unsortedList, sorted))

The type of unsortedList is int[] and the type of sorted is Comparator<Integer>. To make the types match, you need to use Integer[] as the type of unsortedList:

Integer[] unsortedList = {4, 5, 7, 1, 98, 32}; //Expected = 1,4,5,7,32,98

Another problem is that Algorithms.mergeSort returns void, so this still won't compile:

int[] sortedList = Algorithms.mergeSort(unsortedList, sorted);

You need to drop the assignment:

Algorithms.mergeSort(unsortedList, sorted);

Putting it together, this will work (after you implement Algorithms.mergeSort):

public static void main(String[] args) {
    Integer[] unsortedList = {4, 5, 7, 1, 98, 32};

    Comparator<Integer> sorted = Comparator.naturalOrder();
    Algorithms.mergeSort(unsortedList, sorted);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for that detailed answer! :) Now I understood why I have to use Integer instead of int! Have a good day! :)
2

Use an Integer[] instead of int[].

1 Comment

Thank you very much! Problem is solved.. and that simple!
0

Another possibility would also be to work on a clone:

class Algorithm
{
    public static <T> T[] mergeSort(final T[] a, final Comparator<T> c)
    {
        T[] list = a.clone();
        Comparator<T> comp = c;
        Arrays.sort(list, comp);
        return list;
    }

    public static void main(String[] args)
    {
        Integer[] unsortedList = {4,5,7,1,98,32}; //Expected = 1,4,5,7,32,98

        Comparator<Integer> sorted = Comparator.naturalOrder();
        Integer[] sortedList = Algorithm.mergeSort(unsortedList,sorted);
    }
}

Comments

0

If you just want things to compile, you can look at this. There were a few problems:

  1. Trying to assign to mergeSort doesn't work because it's a void method.
  2. Currently your mergeSort does nothing, which you probably know.
  3. The above answer is correct that you need to use Integers.
  4. There were multiple syntax problems, like lack of semicolons and too many parens.

    import java.util.Comparator;
    
    class Algorithms
    {
        public static <T> void mergeSort(final T[] a, final Comparator<T> c)
        {
            T[] list = a;
            Comparator<T> comp = c;
        }
    }
    
    
    public class Main
    {
    
    public static void main(String[] args)
    {
        Integer[] unsortedList = {4,5,7,1,98,32}; //Expected = 1,4,5,7,32,98
    
        Comparator<Integer> sorted = Comparator.naturalOrder();
        Algorithms.mergeSort(unsortedList,sorted);
    }
    }
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.