My program tells me that my method is undefined for my type what does that mean?-Collection of common programming errors
I wrote a code that is supposed to take an array and sort it from smallest value to largest, but I get an error. this is the code
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);
}
}
}
the error that I get is ” The method minHeapify(int[], int, int) is undefined for the type minHeapify” and im not sure what that means.
-
The problem is that the method has the same name as the class and has no return type. Therefore, from the compiler’s point of view, it’s a constructor rather than an ordinary method. And a constructor can’t call itself in the manner your method is trying to.
Rename the method and add a return type. If the method needs to be automatically invoked upon construction, simply call it from the constructor.
-
Java thinks that
public minHeapify(int a[], int start,int end)
is a constructor, not a normal method. You can fix it by respecting the convention that class names are uppercase:public class MinHeapify
.
Originally posted 2013-11-27 12:02:07.