Java 未定义此类型的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29870890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java The method is undefined for this type
提问by Alex mesnier
I am having a problem with calling a method that is in a different class. This main
method is in a class on its own called lab14
, and the heapSort()
method is in a different class called HeapSort
. Both of these classes are in the default package. I am getting the error "The method heapSort(Vector) is undefined for the type Lab14" and I don't understand why, please help.
我在调用不同类中的方法时遇到问题。这个main
方法在一个类中单独调用lab14
,而该heapSort()
方法在另一个类中调用HeapSort
。这两个类都在默认包中。我收到错误消息“未定义 Lab14 类型的 heapSort(Vector) 方法”,我不明白为什么,请帮忙。
below is the main method in the lab 14 class
下面是lab 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);
}
Below is the code for the HeapSort class
下面是 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);
}
}
}
回答by ssantos
You gave the answer:
你给出了答案:
you're trying to call heapSort
method from Lab14
class, but heapSort
method is defined on a different class. The compilation error must be on this line.-
您试图heapSort
从Lab14
类中调用方法,但heapSort
方法是在不同的类上定义的。编译错误必须在这一行。-
heapSort(vec);
You need to instantiate a HeapSort object, and then call its heapSort
method like this.-
您需要实例化一个 HeapSort 对象,然后heapSort
像这样调用它的方法。-
HeapSort myHeapSortObject = new HeapSort();
myHeapSortObject.heapSort(vec);
回答by pathfinderelite
Your heapsort method can be declared static, at least in its current state
您的 heapsort 方法可以声明为静态,至少在其当前状态下
public static <E extends Comparable<? super E>> void heapSort(Vector<StudentGPA> vec) {
You can then access it in your main method like this
然后你可以像这样在你的主方法中访问它
HeapSort.heapSort(vec);
回答by Toby Eggitt
You need to either:
您需要:
1) make the heapSort method public static... and invoke it as HeapSort.heapSort(vec)
1) 将 heapSort 方法设为 public static... 并将其作为 HeapSort.heapSort(vec) 调用
or 2) Invoke the method as new HeapSort().heapSort(vec)
或 2) 调用该方法作为 new HeapSort().heapSort(vec)
回答by YoYo
You have several issues going on here.
您在这里遇到了几个问题。
- You call
heapSort(vec);
from within the staticmain
of classLab14
. This would mean that the compiler expects a method matching signaturestatic void heapSort(Vector<StudentGPA> vec)
within theLab14
class. Up to you how you solve that. - Change
Vector<StudentGPA> vec = new Vector();
intoVector<StudentGPA> vec = new Vector<>();
. Note that addition of the angular brackets<>
. - There are actually many more problems, possibly because it is still an unfinished work. Probably I should stay focused on your original question, for which 1./ is the answer.
- 您
heapSort(vec);
从main
class的静态内部调用Lab14
。这意味着编译器需要static void heapSort(Vector<StudentGPA> vec)
在Lab14
类中匹配签名的方法。取决于你如何解决这个问题。 - 更改
Vector<StudentGPA> vec = new Vector();
成Vector<StudentGPA> vec = new Vector<>();
。注意角括号的添加<>
。 - 其实还有很多问题,可能是因为还没有完成。可能我应该专注于你最初的问题,1./ 是答案。