我有两个用java编写的类,它们是
public class MinHeapify{
public MinHeapify(String a[], int start, int end){
minHeapify(a,start,end);
}
public static void exchange(String a[],int i,int j) {
String 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 static void minHeapify(String a[], int start,int end) {
int l = left(start); int r = right(start);
int smallest=0;
if(l >= end){
smallest = (l < start)? l: start;
}
if(r >= end){
smallest = (r < smallest)? r: smallest;
}
if(smallest != start) {
exchange(a,start,smallest);
minHeapify(a,smallest,end);
}
}
}
import java.util.Scanner;
public class ReservationArray{
private String [] array = new String [50];
public ReservationArray(String [] arg){
int next = 0;
int next2 = 0;
while(arg[++next]!= null){
if(next != arg.length){
arg[next] = array[next2];
next = next++;
next2 = next2++;
}
else if(next == array.length && next!= arg.length){
String [] temp = new String[2*array.length+1];
for(int i=0; i<next; i++){
temp[i] = array[i];
array = temp;
temp = null;
}
} else{
if(next < (array.length/2)-1){
String [] temp = new String[1/2*array.length-1];
for(int i=0; i<next; i++){
temp[i] = array[i];
array = temp;
temp = null;
}
}
}
}
minHeapify(array, 0, array.length-1);
}
我尝试编译时得到的错误是“方法minHeapify(java.lang.String [],int,int)未定义类型ReservationArray”我不明白这意味着什么,我是遗失了什么?
答案 0 :(得分:2)
MinHeapify mh = new MinHeapify();
mh.minHeapify(array, 0, array.length-1);
由于你的方法是静态的,你应该这样调用。
MinHeapify.(array, 0, array.length-1);
答案 1 :(得分:1)
方法minHeapify
属于MinHeapify
类而非ReservationArray
,因此错误。作为static
,您可以使用:
MinHeapify.minHeapify(array, 0, array.length - 1);
或者,您可以使用static
导入:
import static com.your.package.MinHeapify.minHeapify;
并使用当前的非限定方法调用符号。
答案 2 :(得分:1)
将退货类型添加到其中
public void MinHeapify(String a[], int start, int end)
和
MinHeapify.minHeapify(array, 0, array.length - 1);
答案 3 :(得分:0)
嗯minHeapify是一个方法,是类MinHeapify。除非您使用对MinHeapify的引用来访问它,否则您无法在ReservationArray类中访问它。