我是目前从事
Java工作的二年级计算机科学专业的学生,我们最近开始使用仿制药.我有一个任务,我已经获得了一个使用泛型的排序算法列表,我的任务是使用它们来排序整数列表(不是原始的整数).由于排序类使用扩展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
并且提供并期望按原样使用的SelectionSort算法
class SelectionSort> implements SortAlgorithm {
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,它返回一个错误,表明构造函数不能应用于给定的类型.从我在这里和其他地方的搜索中看到的这种问题通常在使用整数时遇到,但我不明白为什么它不能与整数一起工作.
任何关于这个问题的见解都会受到高度赞赏,因为我仍然会接受仿制药的概念.
提前谢谢了!