我正在实现一个HeapSort类,因此它使用addAll方法立即对整个列表进行排序,并将堆中排序的元素重新存储到列表中。这是HeapSort类的驱动程序。为什么我无法创建新的HeapSort对象?我的意思是,当我尝试创建heapsort对象时,我得到错误说“HeapSort是一个原始类型。应该参数化对泛型类型HeapSort的引用。”还有“构造函数HeapSort(Integer [])之类的错误未定义。” 这些是什么意思?为什么我不能做这个对象?
/*demonstrates the HeapSort class so it orders the
* entire list at once using addAll method and re-stores
* the elements ordered in heap into the list*/
public class HeapDemo {
public static void main(String[] args) {
//create a list of integer objects
Integer[] data = {4, 1, 6, 2, 5, 3};
//display elements in list before sorting
System.out.println("Elements in the list before sorting:");
for(int i=0;i<data.length;i++)
System.out.print(data[i] + " ");
System.out.println();
//create object for HeapSort class
HeapSort first = new HeapSort(data);
//display elements in list after sorting
System.out.println("\nElements in the list after sorting:");
for(int i=0;i<data.length;i++)
System.out.print(data[i] + " ");
System.out.println();
}
}
这是我的HeapSort课程:
/**
* HeapSort sorts a given array of Comparable objects using a heap.
*
* @author Java Foundations
* @version 4.0
*/
public class HeapSort<T>
{
/**
* Sorts the specified array using a Heap
*
* @param data the data to be added to the heapsort
*/
ArrayHeap heap;
public void HeapSort(T[] data)
{
ArrayHeap<T> temp = new ArrayHeap<T>();
addAll(data);
// copy the array into a heap
/*
* for (int i = 0; i < data.length; i++)
temp.addElement(data[i]);*/
// place the sorted elements back into the array
int count = 0;
while (!(temp.isEmpty()))
{
data[count] = temp.removeMin();
count++;
}
}
//accepts a list of elements and stores all elements into heap
private void addAll(T[] list) {
for(int i=0;i<list.length;i++)
heap.addElement(list[i]);
}
}
答案 0 :(得分:1)
你没有一个接受数组的构造函数。方法名称应以小写字母开头,并且绝对不应该与类名称相同。构造者没有返回类型。
public void HeapSort(T[] data)
不是构造函数。