如何修复方法无法应用于给定类型的问题。

huangapple go评论69阅读模式
英文:

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&lt;n;i++){
            arr[i] = i;
        }
        long startTime = System.currentTimeMillis();
		for(int t= 0; t&lt;z; t++) {
		binarySearch();
		}
		long stopTime = System.currentTimeMillis();
		long elapsedTime = stopTime - startTime;
		System.out.println(&quot;It took &quot; + elapsedTime + &quot; ms to repeat the algorithm.&quot;);
		}
		int binarySearch(int n, double k, int arr[]) {
			int li = 0;
			int re = n+1;
			int m;
			while (li &lt;  re-1) {
				m = (li + re) / 2;
				if (k &lt;=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] = &quot;100&quot;;
        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&lt;n;i++){
            arr[i] = i;
        }
        long startTime = System.currentTimeMillis();
        for(int t= 0; t&lt;z; t++) {
        binarySearch(n, k, arr);
        }
        long stopTime = System.currentTimeMillis();
        long elapsedTime = stopTime - startTime;
        System.out.println(&quot;It took &quot; + elapsedTime + &quot; ms to repeat the algorithm.&quot;);
        }
    
    static int binarySearch(int n, double k, int arr[]) {
            int li = 0;
            int re = n+1;
            int m;
            while (li &lt;  re-1) {
                m = (li + re) / 2;
                if (k &lt;=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

huangapple
  • 本文由 发表于 2020年6月6日 00:54:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/62220447.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定