英文:
How to fix method cannot be applied to given types
问题
我正在创建一个用于执行二分搜索的程序。
该方法应该重复执行不同次数,并且我希望打印出重复执行该方法所花费的时间。
目前代码的方式,我在编译方法"binary search"时会出现错误,无法编译给定的类型,我不知道如何修复它。
public class Bin_search {
public static void main(String[] args) {
int z = Integer.parseInt(args[0]);
double k = (int)(Math.random()*1000001);
int n = 1000000;
int arr[] = new int[n];
int i = 0;
for(i = 0; i < n; i++){
arr[i] = i;
}
long startTime = System.currentTimeMillis();
for(int t = 0; t < z; t++) {
binarySearch(n, k, arr);
}
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println("重复执行该算法所花费的时间为:" + elapsedTime + " 毫秒。");
}
static int binarySearch(int n, double k, int arr[]) {
int li = 0;
int re = n + 1;
int m;
while (li < re - 1) {
m = (li + re) / 2;
if (k <= arr[m]) {
re = m;
} else {
li = m;
}
}
return re;
}
}
英文:
I am creating a program to do a binary search.
The method should be repeated a different number of times and and i want to print out the time it took to repeat the method.
They way the code is now i get an error while compiling method binary search cannot be compiled to given types and i dont know how to fix it.
public class Bin_search {
public static void main(String[] args) {
int z = Integer.parseInt(args[0]);
double k = (int)(Math.random()*1000001);
int n = 1000000;
int arr[] = new int[n];
int i = 0;
for(i = 0;i<n;i++){
arr[i] = i;
}
long startTime = System.currentTimeMillis();
for(int t= 0; t<z; t++) {
binarySearch();
}
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println("It took " + elapsedTime + " ms to repeat the algorithm.");
}
int binarySearch(int n, double k, int arr[]) {
int li = 0;
int re = n+1;
int m;
while (li < re-1) {
m = (li + re) / 2;
if (k <=arr[m]){
re = m;
}
else{
li = m;
}
}
return re;
}
}
答案1
得分: 1
首先,binarySearch 方法由三个参数构成,而您并没有提供它们;其次,您应该将 binarySearch 方法声明为静态的,否则在不首先创建实例的情况下无法从 main 方法中调用它。
应该像这样修改:
public class Main {
public static void main(String[] args) {
args = new String[3];
args[0] = "100";
int z = Integer.parseInt(args[0]);
double k = (int)(Math.random() * 1000001);
int n = 1000000;
int arr[] = new int[n];
int i = 0;
for (i = 0; i < n; i++) {
arr[i] = i;
}
long startTime = System.currentTimeMillis();
for (int t = 0; t < z; t++) {
binarySearch(n, k, arr);
}
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println("It took " + elapsedTime + " ms to repeat the algorithm.");
}
static int binarySearch(int n, double k, int arr[]) {
int li = 0;
int re = n + 1;
int m;
while (li < re - 1) {
m = (li + re) / 2;
if (k <= arr[m]) {
re = m;
} else {
li = m;
}
}
return re;
}
}
编辑: 现在它可以工作了,但请验证您的应用逻辑以确保它执行的是您所期望的操作。
英文:
First of all, the binarySearch method is made by three arguments, and you are not providing them, secondly you should make your binarySearch method static, otherwise it cannot be called from the main method without creating an instance first.
It should be like this, i think
public class Main {
public static void main(String[] args) {
args = new String[3];
args[0] = "100";
int z = Integer.parseInt(args[0]);
double k = (int)(Math.random() * 1000001);
int n = 1000000;
int arr[] = new int[n];
int i = 0;
for(i = 0;i<n;i++){
arr[i] = i;
}
long startTime = System.currentTimeMillis();
for(int t= 0; t<z; t++) {
binarySearch(n, k, arr);
}
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println("It took " + elapsedTime + " ms to repeat the algorithm.");
}
static int binarySearch(int n, double k, int arr[]) {
int li = 0;
int re = n+1;
int m;
while (li < re-1) {
m = (li + re) / 2;
if (k <=arr[m]){
re = m;
}
else{
li = m;
}
}
return re;
}
}
EDIT: Now it works, but please check if your application logic to be sure it's doing what you are expecting to do
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论