我有以下HeapSort方法
public static <T extends Comparable<T>> void sort(List<T> in, List<T> out) {
Heap<T> heap = new Heap<T>();
Iterator<T> it = in.iterator();
while (it.hasNext()) {
heap.add(it.next());
}
while (!heap.isEmpty()) {
out.insertLast(heap.remove());
}
}
在我的Heap<T extends Comparable<T>>
课程中。我现在正在尝试使用它来对整数列表进行排序,但是我收到了这个错误:
The method sort(List<T>, List<T>) in the type Heap<Integer> is not applicable for the arguments (List, List)
这是主要方法:
public class HeapSortTester {
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args) {
Heap<Integer> heap = new Heap<>();
List<Integer> unsortedList = new ArrayList();
List<Integer> sortedList = new ArrayList();
for (int i = 0; i < 1000; i++) {
int number = (int) (Math.random() * 1000) + 1;
unsortedList.add(number);
}
heap.sort(unsortedList, sortedList); //ERROR GIVEN HERE
System.out.println("Unsorted List: " + unsortedList + "\n");
System.out.println("Sorted List: " + sortedList + "\n");
}
}
HeapSortTester
是否应该延伸Comparable<T>
或其他内容?或者我如何允许sort方法与Integer
类型一起使用,同时保持其定义通用?
答案 0 :(得分:0)
更改
List<Integer> unsortedList = new ArrayList<>();
List<Integer> sortedList = new ArrayList<>();
到
<>
请注意用于实例化ArrayList
的菱形运算符({{1}})。