我的程序告诉我,我的方法未定义我的类型是什么意思?

时间:2013-03-29 15:07:54

标签: java compiler-errors

我写了一个代码,它应该采用一个数组并将其从最小值排序到最大值,但是我得到一个错误。这是代码

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”并且我不确定这意味着什么。

2 个答案:

答案 0 :(得分:2)

问题是该方法与类具有相同的名称,并且没有返回类型。因此,从编译器的角度来看,它是一个构造函数而不是普通的方法。并且构造函数不能以您的方法尝试的方式调用自身。

重命名方法并添加返回类型。如果需要在构造时自动调用该方法,只需从构造函数中调用它。

答案 1 :(得分:1)

Java认为public minHeapify(int a[], int start,int end)是构造函数,而不是普通方法。您可以通过遵守类名称为大写的约定来修复它:public class MinHeapify