如何使用eclipse运行《算法》第四版中BinarySearch.java

        对于普林斯顿大学的公开课《算法》中的相关程序,课程中提供的的drjava。由于习惯问题,很多人会采用eclipse的编译环境,但很多程序需要输入txt文件作为输出,一种方法是使用命令行输入,一种是配置run configuration。命令行不多介绍,网上较多说明,这里主要说明 run configuration配置。
        以BinarySearch.java为例,前面关于algs4包的配置不予多讲,可百度eclipse创建project、如何添加包。首先将algs4-data.zip的数据集下载,解压到与你的project中src文件同级的目录中。

    为了方便读取,需改动main函数中读取部分的代码:

 public static void main(String[] args) {

        // read the integers from a file
        In in = new In(args[0]);
        int[] whitelist = in.readAllInts();//读取第一个文件
        In in2 = new In(args[1]);
        int[] whitelist2 = in2.readAllInts();//读取第二个文件
        // sort the array
        Arrays.sort(whitelist);

        // read integer key from the second file; print if not in whitelist

        for(int i=0;i<whitelist2.length;i++){
            int key = whitelist2[i];
            if (BinarySearch.indexOf(whitelist, key) == -1)
                StdOut.println(key);
        }
}

run configuration如下,此时必须保证已经将数据集解压到与你的project中src文件同级的目录中。

   点击运行即可。

猜你喜欢

转载自blog.csdn.net/qq_35423500/article/details/80931928