Java此类型的方法未定义

问题描述 投票:0回答:4

我在调用不同类中的方法时遇到问题。这个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);
    }

}


}
java class heapsort
4个回答
0
投票

你给出了答案:

你试图从heapSort类调用Lab14方法,但是heapSort方法是在不同的类上定义的。编译错误必须在此行上.-

heapSort(vec);

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

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

0
投票

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

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

然后,您可以在主方法中访问它

HeapSort.heapSort(vec);

0
投票

你需要:

1)使heapSort方法公共静态...并将其作为HeapSort.heapSort(vec)调用

或2)将方法调用为新的HeapSort()。heapSort(vec)


0
投票

你有几个问题在这里发生。

  1. 你从heapSort(vec);类的静态main中调用Lab14。这意味着编译器需要在static void heapSort(Vector<StudentGPA> vec)类中匹配签名Lab14的方法。由您决定如何解决这个问题。
  2. Vector<StudentGPA> vec = new Vector();改为Vector<StudentGPA> vec = new Vector<>();。请注意,添加角括号<>
  3. 实际上还有很多问题,可能是因为它仍然是一项未完成的工作。可能我应该专注于你原来的问题,其中1. /是答案。
© www.soinside.com 2019 - 2024. All rights reserved.