简体   繁体   English

实现Java比较器的问题-泛型

[英]Problems with implementing Java Comparator - Generics

So, I have got a problem in my Main class when I would like to call the mergeSort() method caused by the Comparator. 因此,当我想调用由Comparator引起的mergeSort()方法时,我的Main类出现了问题。 I get the following 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))
    }
}

In this code, the types don't match in the Algorithms.mergeSort call: 在此代码中,类型在Algorithms.mergeSort调用中不匹配:

 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> . unsortedList的类型为int[]sorted的类型为Comparator<Integer> To make the types match, you need to use Integer[] as the type of unsortedList : 为了使类型匹配,您需要使用Integer[]作为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: 另一个问题是Algorithms.mergeSort返回void ,因此仍然无法编译:

 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 ): 放在一起,这将起作用(在实现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);
}

使用Integer[]而不是int[]

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);
    }
}

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. 尝试分配给mergeSort无效,因为它是一个无效方法。
  2. Currently your mergeSort does nothing, which you probably know. 当前,您的mergeSort不执行任何操作,您可能知道。
  3. The above answer is correct that you need to use Integers. 上面的答案是正确的,您需要使用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); } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM