如何从 Robert Sedgewick 的算法教科书中运行 Java 程序?

我为一个菜鸟问题道歉。我正在尝试在 Eclipse 中运行由 Robert Sedgewick 和 Kevin Wayne 编写的算法第 4 版书中给出的 Java 程序:https ://algs4.cs.princeton.edu/home/


我在输入程序的参数时遇到问题。


例如对于以下程序:


import java.util.Arrays;


public class BinarySearch

{

    public static int rank(int key, int[] a)

    { // Array must be sorted.

     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)

    {


        int[] whitelist = In.readInts(args[0]);

        Arrays.sort(whitelist);

        while (!StdIn.isEmpty())

        { // Read key, print if not in whitelist.        

         int key = StdIn.readInt();

            if (rank(key, whitelist) < 0)

            StdOut.println(key);

        }    

    }

}

输入参数是:


% java BinarySearch tinyW.txt < tinyT.txt

我不知道在 Eclipse 中在哪里传递输入参数。任何帮助将不胜感激。


智慧大石
浏览 115回答 3
3回答

隔江千里

进入“运行配置...”打开播放按钮的菜单。您可以在参数、环境选项卡和常用选项中找到所需的内容。实际上常见的是您需要的选项卡。

慕侠2389804

我解决了。显然,图书网站上提供的较新的图书馆与我所拥有的书有所不同。我将主要功能更改如下:import java.util.Arrays;public class BinarySearch{public static int rank(int key, int[] a){ // Array must be sorted.&nbsp; &nbsp; int lo = 0;&nbsp; &nbsp; int hi = a.length - 1;&nbsp; &nbsp; while (lo <= hi)&nbsp; &nbsp; { // Key is in a[lo..hi] or not present.&nbsp; &nbsp; &nbsp; &nbsp; int mid = lo + (hi - lo) / 2;&nbsp; &nbsp; &nbsp; &nbsp; if (key < a[mid]) hi = mid - 1;&nbsp; &nbsp; &nbsp; &nbsp; else if (key > a[mid]) lo = mid + 1;&nbsp; &nbsp; &nbsp; &nbsp; else return mid;&nbsp; &nbsp; }&nbsp; &nbsp; return -1;}public static void main(String[] args){&nbsp; &nbsp; In i = new In(args[0]);&nbsp; &nbsp; In j = new In(args[1]);&nbsp; &nbsp; int[] whitelist = i.readAllInts();&nbsp; &nbsp; int[] iplist = j.readAllInts();&nbsp; &nbsp; Arrays.sort(whitelist);&nbsp; &nbsp; for (int key:iplist)&nbsp; &nbsp; { // Read key, print if not in whitelist.&nbsp; &nbsp; &nbsp; &nbsp; if (rank(key, whitelist) < 0)&nbsp; &nbsp; &nbsp; &nbsp; StdOut.println(key);&nbsp; &nbsp; }}}然后通过"tinyW.txt" "tinyT.txt"作为 @GDG612 指示的程序参数。

小怪兽爱吃肉

右键单击您的程序并选择Run As -> Run Configurations...&nbsp;然后单击(x)= Arguments选项卡并将输入传递给程序下Program arguments
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java