首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

algorithm(java语言实现的) 第四版 小弟我运行异常 求大侠指点

2013-09-06 
algorithm(java语言实现的) 第四版 我运行错误 求大侠指点/********************************************

algorithm(java语言实现的) 第四版 我运行错误 求大侠指点


/*************************************************************************
 *  Compilation:  javac BinarySearch.java
 *  Execution:    java BinarySearch whitelist.txt < input.txt
 *  Data files:   http://algs4.cs.princeton.edu/11model/tinyW.txt
 *                http://algs4.cs.princeton.edu/11model/tinyT.txt
 *                http://algs4.cs.princeton.edu/11model/largeW.txt
 *                http://algs4.cs.princeton.edu/11model/largeT.txt
 *
 *  % java BinarySearch tinyW.txt < tinyT.txt
 *  50
 *  99
 *  13
 *
 *  % java BinarySearch largeW.txt < largeT.txt | more
 *  499569
 *  984875
 *  295754
 *  207807
 *  140925
 *  161828
 *  [3,675,966 total values]
 *  
 *************************************************************************/

import java.util.Arrays;

public class BinarySearch {

    // precondition: array a[] is sorted
    public static int rank(int key, int[] a) {
        int lo = 0;
        int hi = a.length - 1;
        while (lo <= hi) {
            // Key is in a[lo..hi] or not present.
            int mid = lo + (hi - lo) / 2;
            if      (key < a[mid]) hi = mid - 1;
            else if (key > a[mid]) lo = mid + 1;
            else return mid;
        }
        return -1;


    }

    public static void main(String[] args) {
        In in = new In(args[0]);
        int[] whitelist = in.readAllInts();

        Arrays.sort(whitelist);

        // read key; print if not in whitelist
        while (!StdIn.isEmpty()) {
            int key = StdIn.readInt();
            if (rank(key, whitelist) == -1)
                StdOut.println(key);
        }
    }
}


出现错误Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at BinarySearch.main(BinarySearch.java:44)  44行的代码是 In in = new In(args[0]);

求大侠指点迷津,这个对小弟很重要
algorithm java exception string thread
[解决办法]
你的运行参数是啥?
[解决办法]
你没带参数运行
[解决办法]
java BinarySearch 1 2 3
去研究一下args是个什么玩意儿吧。。
[解决办法]
传给args[0]的参数是什么呢?
String[] args这个字符串数组是保存运行main函数时输入的参数 的,例如 main函数所在的类名为test那么你在cmd运行java test a b c时 args[0]=a,args[1]=b,args[2]=c,你就可以在你的程序中调用你输入的这些变量。 
[解决办法]
在Eclipse里以Run configurations方式运行,配一下program arguments就行了( 如果IDE是eclipse)

热点排行