有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

类Java此类型的方法未定义

我在调用另一个类中的方法时遇到问题。这个main方法位于一个名为lab14的类中,而heapSort()方法位于另一个名为HeapSort的类中。这两个类都在默认包中。我得到错误“heapSort(Vector)方法对于Lab14类型是未定义的”,我不明白为什么,请帮助

以下是第14实验课的主要方法

public static void main(String args[]) {

    Heap myheap = new Heap();
    Vector<StudentGPA> vec = new Vector();
    int []  br = new int[20]; 
    double []  dr = new double[20]; 
    int i = 0;
    int j = 0;
    try {
    //String inputLine; //stores each line from the file
    Scanner scanLine;
    Scanner input = new Scanner(new File("students.in"));
    while (input.hasNextLine()){
    scanLine = new Scanner(input.nextLine());

    int id = scanLine.nextInt();
    //br[i] = id;
    String name = scanLine.next();
    double gpa = scanLine.nextDouble();
    //dr[i]= gpa;
    //myStr.add(name);
    if(scanLine.hasNext())
    {
         String advisor = scanLine.next();
         GraduateStudentGPA grad = new GraduateStudentGPA(id,name,gpa,advisor);
         vec.add(grad);
    }
    else
    {
        StudentGPA reg = new StudentGPA(id,name,gpa);
        vec.add(reg);
    }
    i++;
    j++;
    }
    input.close();
} 
catch (IOException e) {
    System.out.println("IOException in reading input file!!!"+e);
}   
heapSort(vec);
}

下面是HeapSort类的代码

public class HeapSort <E extends Comparable<? super E>>{
/** sorts the input vector using heap Sort <ul> <li> iterates
 * through each element of the input vector and inserts each
 * element to the heap by calling {\tt heapInsert}.  <li> deletes
 * each of the inserted items by calling {\tt heapDelete} the
 * appropriate number of times, and fills up the vector with the
 * returned elements.  </ul> If you are using the
 * minheap implementation, this insertion and deletion of all
 * items will produce a list of items sorted by their key
 * attribute values.
 * @param vec input vector
 */
public void heapSort(Vector<StudentGPA> vec){
    //  --  TO COMPLETE  --
    Heap myheap = new Heap<E>();
    for(int i = 0; i <vec.size(); i++)
    {
        myheap.heapInsert(vec.elementAt(i));
    }
    for(int i = 0; i <vec.size(); i++)
    {
        vec.setElementAt((StudentGPA) myheap.heapDelete(), i);
    }

}


}

共 (4) 个答案

  1. # 1 楼答案

    你需要:

    1)公开heapSort方法静态。。。并将其称为HeapSort。希普索尔(vec)

    或者 2) 以new HeapSort()的形式调用该方法。希普索尔(vec)

  2. # 2 楼答案

    这里有几个问题

    1. 从类Lab14的静态main中调用heapSort(vec);。这意味着编译器需要一个与Lab14类中的签名static void heapSort(Vector<StudentGPA> vec)匹配的方法。这取决于你如何解决
    2. Vector<StudentGPA> vec = new Vector();更改为Vector<StudentGPA> vec = new Vector<>();。请注意,添加了角括号<>
    3. 实际上还有很多问题,可能是因为这仍然是一项未完成的工作。也许我应该把注意力集中在你最初的问题上,因为这就是答案
  3. # 3 楼答案

    heapsort方法可以声明为静态,至少在当前状态下是静态的

    public static <E extends Comparable<? super E>> void heapSort(Vector<StudentGPA> vec) {
    

    然后,您可以像这样通过主方法访问它

    HeapSort.heapSort(vec);
    
  4. # 4 楼答案

    你给出了答案:

    您试图从Lab14类调用heapSort方法,但heapSort方法是在另一个类上定义的。编译错误必须在这一行。-

    heapSort(vec);
    

    你需要实例化一个HeapSort对象,然后像这样调用它的heapSort方法

    HeapSort myHeapSortObject = new HeapSort();
    myHeapSortObject.heapSort(vec);