【问题标题】:Problems with implementing Java Comparator - Generics实现 Java 比较器的问题 - 泛型
【发布时间】:2023-03-19 20:03:01
【问题描述】:

所以,当我想调用由 Comparator 引起的 mergeSort() 方法时,我的 Main 类出现了问题。我收到以下消息:

我不知道如何解决这个问题..请帮助我!

注意: 不要怀疑代码中没有发生任何事情。我被卡住了,因为由于上述问题,我无法证明我的代码的功能:(

(对不起我的英语不好)

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

【问题讨论】:

  • 也无助于您期望来自 mergeSort 的返回值,这是一个 void 方法。
  • 是的,你是对的!我认为这是在尝试很多事情来解决这个问题时发生的......:D

标签: java generics initialization implementation comparator


【解决方案1】:

在此代码中,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))

unsortedList 的类型是int[]sorted 的类型是Comparator&lt;Integer&gt;。要使类型匹配,您需要使用Integer[] 作为unsortedList 的类型:

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

另一个问题是Algorithms.mergeSort 返回void,所以这仍然无法编译:

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

你需要放弃作业:

Algorithms.mergeSort(unsortedList, sorted);

把它放在一起,这会起作用(在你实现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!祝你有美好的一天! :)
【解决方案2】:

使用Integer[] 代替int[]

【讨论】:

  • 非常感谢!问题解决了……就这么简单!
【解决方案3】:

另一种可能性是在克隆上工作:

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

【讨论】:

    【解决方案4】:

    如果你只是想让东西编译,你可以看看这个。有几个问题:

    1. 尝试分配给 mergeSort 不起作用,因为它是一个 void 方法。
    2. 目前您的 mergeSort 什么都不做,您可能知道。
    3. 上面的答案是正确的,你需要使用整数。
    4. 存在多个语法问题,例如缺少分号和过多的括号。

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

    【讨论】:

      猜你喜欢
      • 2021-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-06
      • 2011-01-15
      相关资源
      最近更新 更多