I'm a second year computer science student currently working in Java and we recently started generics. I have an assignment where I've been given a list of sorting algorithms that use generics and am tasked with using them to sort a list of Integers (not primitive ints). As the sort classes use generics that extend Comparable I thought there would be no problems simply handing them the Integer array but the build output keeps coming up with incompatible types.
我是目前从事Java工作的二年级计算机科学专业的学生,我们最近开始使用仿制药。我有一个任务,我已经给了一个使用泛型的排序算法列表,我的任务是使用它们来排序整数列表(不是原始的int)。由于排序类使用扩展Comparable的泛型,我认为只需将它们交给Integer数组就没有问题,但构建输出不断出现不兼容的类型。
The relevant code is below;
相关代码如下;
portion of Main program
主程序的一部分
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
And the SelectionSort algorithm which is provided and expected to be used as-is
并且提供并期望按原样使用的SelectionSort算法
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
The problem I'm having is in declaring SelectionSort, which returns an error that the constructor cannot be applied to the given type. From what I've read in my searches here and elsewhere this kind of problem is usually encountered when using ints, but I don't understand why it isn't working with Integers. Any insight on the problem would be greatly appreciated as I'm still coming to terms with the concept of generics. Many thanks in advance!
我遇到的问题是声明SelectionSort,它返回一个错误,表明构造函数不能应用于给定的类型。从我在这里和其他地方的搜索中读到的这种问题通常在使用int时遇到,但我不明白为什么它不能与Integers一起使用。任何关于这个问题的见解都会受到高度赞赏,因为我仍然会接受仿制药的概念。提前谢谢了!
5 个解决方案
#1
3
This should fix the problem:
这应该解决问题:
SelectionSort<Integer> ss = new SelectionSort<Integer>();
ss.sort(list, dataSize);
You were trying to pass arguments into a constructor that didn't exist, when you want to pass them into the sort
method instead.
当您想要将参数传递给sort方法时,您试图将参数传递给不存在的构造函数。
Here I'm using the default (no-arg) constructor to instantiate a new SelectionSort<Integer>
, assigning it to a variable ss
, then calling sort
on that instance with the arguments.
在这里,我使用默认(no-arg)构造函数来实例化一个新的SelectionSort
Also note that if you just need the instance to call sort
, you can skip the assignment:
另请注意,如果您只需要实例来调用sort,则可以跳过赋值:
new SelectionSort<Integer>().sort(list, dataSize);
#2
3
Your SelectionSort
class is generic. You should specify the type parameter when you declare and instantiate one of them:
您的SelectionSort类是通用的。在声明并实例化其中一个时,应指定type参数:
SelectionSort<Integer> SS = new SelectionSort<Integer>(list, dataSize);
In Java 7, you can rely on type inference to shorten this a bit:
在Java 7中,您可以依靠类型推断来缩短这一点:
SelectionSort<Integer> SS = new SelectionSort<>(list, dataSize);
#3
3
SelectionSort SS = new SelectionSort(list, dataSize);//problem is here
should be
SelectionSort<Integer> SS = new SelectionSort<Integer>(list, dataSize);//no problem now
your SelectionSort has a parameterized type (sometype which implements comparable). java.lang.Integer
implements Comparable
.
您的SelectionSort具有参数化类型(某种类型实现可比较)。 java.lang.Integer实现了Comparable。
#4
2
SelectionSort SS = new SelectionSort(list, dataSize);
Needs to be changed to:
需要改为:
SelectionSort<Integer> SS = new SelectionSort<Integer>(list, dataSize);
You have to declare the parameterized type when you create the object
您必须在创建对象时声明参数化类型
#5
1
In addition to the generics issue already covered, I think the code confuses the constructor and the sort method. Change the failing line to:
除了已经涵盖的泛型问题之外,我认为代码会混淆构造函数和排序方法。将失败的行更改为:
SelectionSort<Integer> SS = new SelectionSort<Integer>();
SS.sort(list, dataSize);
The code does not show a SelectionSort constructor, only a sort method which expects the parameters that were being passed to the constructor. The error message is consistent with SelectionSort only having a parameterless constructor supplied by default by the compiler.
代码不显示SelectionSort构造函数,只显示一个排序方法,该方法需要传递给构造函数的参数。错误消息与SelectionSort一致,只有编译器默认提供无参数构造函数。
#1
3
This should fix the problem:
这应该解决问题:
SelectionSort<Integer> ss = new SelectionSort<Integer>();
ss.sort(list, dataSize);
You were trying to pass arguments into a constructor that didn't exist, when you want to pass them into the sort
method instead.
当您想要将参数传递给sort方法时,您试图将参数传递给不存在的构造函数。
Here I'm using the default (no-arg) constructor to instantiate a new SelectionSort<Integer>
, assigning it to a variable ss
, then calling sort
on that instance with the arguments.
在这里,我使用默认(no-arg)构造函数来实例化一个新的SelectionSort
Also note that if you just need the instance to call sort
, you can skip the assignment:
另请注意,如果您只需要实例来调用sort,则可以跳过赋值:
new SelectionSort<Integer>().sort(list, dataSize);
#2
3
Your SelectionSort
class is generic. You should specify the type parameter when you declare and instantiate one of them:
您的SelectionSort类是通用的。在声明并实例化其中一个时,应指定type参数:
SelectionSort<Integer> SS = new SelectionSort<Integer>(list, dataSize);
In Java 7, you can rely on type inference to shorten this a bit:
在Java 7中,您可以依靠类型推断来缩短这一点:
SelectionSort<Integer> SS = new SelectionSort<>(list, dataSize);
#3
3
SelectionSort SS = new SelectionSort(list, dataSize);//problem is here
should be
SelectionSort<Integer> SS = new SelectionSort<Integer>(list, dataSize);//no problem now
your SelectionSort has a parameterized type (sometype which implements comparable). java.lang.Integer
implements Comparable
.
您的SelectionSort具有参数化类型(某种类型实现可比较)。 java.lang.Integer实现了Comparable。
#4
2
SelectionSort SS = new SelectionSort(list, dataSize);
Needs to be changed to:
需要改为:
SelectionSort<Integer> SS = new SelectionSort<Integer>(list, dataSize);
You have to declare the parameterized type when you create the object
您必须在创建对象时声明参数化类型
#5
1
In addition to the generics issue already covered, I think the code confuses the constructor and the sort method. Change the failing line to:
除了已经涵盖的泛型问题之外,我认为代码会混淆构造函数和排序方法。将失败的行更改为:
SelectionSort<Integer> SS = new SelectionSort<Integer>();
SS.sort(list, dataSize);
The code does not show a SelectionSort constructor, only a sort method which expects the parameters that were being passed to the constructor. The error message is consistent with SelectionSort only having a parameterless constructor supplied by default by the compiler.
代码不显示SelectionSort构造函数,只显示一个排序方法,该方法需要传递给构造函数的参数。错误消息与SelectionSort一致,只有编译器默认提供无参数构造函数。