我写了一个代码,它应该接受一个数组并将它从最小值到最大值排序,但是我得到了一个错误。这是代码
public class minHeapify{
public static void exchange(int a[],int i,int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static int parent(int i) {
return (int) Math.floor((i - 1)/2);
}
public static int left(int i) {
return 2*i + 1;
}
public static int right(int i) {
return 2*(i+1);
}
public minHeapify(int a[], int start,int end) {
int l = left(start); int r = right(start);
int smallest;
if(l >= end){
smallest = (a[l] < a[start])? l: start;
}
if(r >= end){
smallest = (a[r] < a[smallest])? r: smallest;
}
if(smallest != start) {
exchange(a,start,smallest);
minHeapify(a,smallest,end);
}
}
}
我得到的错误是“方法 minHeapify(int[], int, int) 未定义类型 minHeapify”,我不确定这意味着什么。
最佳答案
问题是该方法与类同名并且没有返回类型。因此,从编译器的角度来看,它是一个构造函数而不是一个普通的方法。并且构造函数不能以您的方法尝试的方式调用自身。
重命名方法并添加返回类型。如果需要在构造时自动调用该方法,只需从构造函数中调用即可。
关于java - 我的程序告诉我,我的方法未针对我的类型定义,这是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15705922/