在驱动程序类中为堆排序实现创建对象

问题描述:

我正在实现HeapSort类,因此它使用addAll方法一次排列整个列表,并将堆中排序的元素重新存储到列表中。这是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 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

你有什么错误? – Mureinik

+0

没有任何类型的错误消息,我们不能真正说。 –

+0

@Mureinik抱歉,我现在更新了 –

你没有一个构造函数的数组。方法名称应以小写字母开头,绝对不应该与该类名称相同。构造函数没有返回类型。

public void HeapSort(T[] data) 

不是构造函数。

+0

是的你是对的,我现在已经改变了它。谢谢。 –

+0

但现在我的错误说在线程“主”java.lang.NullPointerException异常 \t在HeapSort.addAll(HeapSort.java:38) \t在HeapSort。 (HeapSort.java:21) \t at HeapDemo.main(HeapDemo.java:18) –

+0

为什么当我运行驱动程序时会发生这种情况? –