使用泛型在Java中对整数数组进行排序

5
我是一名大二计算机科学专业的学生,目前正在学习Java编程语言,并且最近开始学习泛型。我有一个作业,要求使用给定的泛型排序算法对一个整数列表进行排序(不是原始类型int)。由于排序类使用了扩展Comparable的泛型,所以我认为只需将Integer数组传递给它们就没有问题了,但构建输出不断出现不兼容类型的错误。
相关代码如下:
主程序部分
final int NUMITEMS = 100000;
Integer[] list = new Integer[NUMITEMS];
int dataSize = 0;

//method reads contents of a file into array and returns number of objects
System.out.println((dataSize = readDataFile(list)));

SelectionSort SS = new SelectionSort(list, dataSize);//problem is here

同时也提供了选择排序算法,期望使用不作修改。

class SelectionSort<T extends Comparable<? super T>> implements SortAlgorithm<T>  {

public void  sort ( T [ ] theArray,   int size ) {

  for (int last = size-1; last > 0 ; last--)
  {
     int largest = 0;
     for (int scan = 1; scan <= last; scan++)
        if (theArray[scan].compareTo(theArray[largest])>0)
           largest = scan;

     /** Swap the values */
     T temp = theArray[largest];
     theArray[largest] = theArray[last];
     theArray[last] = temp;
  }
} // method selectionSort

我遇到的问题是在声明SelectionSort时,出现了一个错误,提示无法将构造函数应用于给定的类型。据我在这里和其他地方搜索得知,通常使用int时会遇到这种问题,但我不明白为什么Integer也不能工作。 如果您能就这个问题提供任何见解,我将非常感激,因为我还在逐渐理解泛型的概念。 非常感谢!
5个回答

3

您的 SelectionSort 类是通用的。在声明和实例化其中之一时,您应该指定类型参数:

SelectionSort<Integer> SS = new SelectionSort<Integer>(list, dataSize);

在Java 7中,您可以依靠类型推断来缩短一点:
SelectionSort<Integer> SS = new SelectionSort<>(list, dataSize);

啊,当然!我完全忘记了那是必要的!但即使进行了这些更改,问题仍然存在。构建输出为:错误:类SelectionSort<T>中的构造函数SelectionSort无法应用于给定类型; SelectionSort<Integer> SS = new SelectionSort<Integer> (list, dataSize); ^ 需要:无参数 找到:Integer[],int 原因:实际和形式参数列表长度不同 其中T是类型变量: T extends Comparable <? super T>在类SelectionSort中声明 1个错误 - JorC
@JorC SelectionSort类只有一个默认构造函数。您的列表和数据大小将进入一个名为sort的方法调用。 - Michael Krussel
搞定了。我简直不敢相信自己竟然忽略了这么微不足道的东西。已经修复好了,现在运行得很好。感谢大家的帮助! - JorC
1
@JorC - 很高兴它被修复了。看起来,你可以使用通用方法(而不必担心通用类)。 - Ted Hopp
@TedHopp 我也是这么想的,但看起来 OP 可能需要实现 SortAlgorithm<T> 接口。 - Paul Bellora

3
SelectionSort SS = new SelectionSort(list, dataSize);//problem is here

应该是

SelectionSort<Integer> SS = new SelectionSort<Integer>(list, dataSize);//no problem now

你的选择排序有一个带参数的类型(某种实现比较接口的类型)。java.lang.Integer 实现了 Comparable 接口。


3
这应该可以解决问题:
SelectionSort<Integer> ss = new SelectionSort<Integer>();
ss.sort(list, dataSize);

您试图传递参数到一个不存在的构造函数中,而应该将它们传递到sort方法中。这里我使用默认(无参)构造函数来实例化一个新的SelectionSort<Integer>,将其分配给变量ss,然后在该实例上调用带有参数的sort方法。此外,请注意,如果您只需要实例来调用sort,则可以跳过赋值步骤。
new SelectionSort<Integer>().sort(list, dataSize);

2
SelectionSort SS = new SelectionSort(list, dataSize);

Needs to be changed to:

SelectionSort<Integer> SS = new SelectionSort<Integer>(list, dataSize);

你需要在创建对象时声明参数化类型。

我注意到初学者在使用泛型时往往会将所有与类型相关的编译器错误消息都归因于泛型。这使得像这样简单的问题很容易被忽略。 - Patricia Shanahan

1
除了已经涵盖的通用问题之外,我认为代码混淆了构造函数和排序方法。将失败的行更改为:
SelectionSort<Integer> SS = new SelectionSort<Integer>();
SS.sort(list, dataSize);

代码中没有显示SelectionSort构造函数,只有一个sort方法,该方法期望传递给构造函数的参数。错误消息与SelectionSort仅由编译器默认提供无参数构造函数一致。

Paul Bellora看到了和我一样的问题,并在我之前发布了他的答案。 - Patricia Shanahan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接