对于一个菜鸟问题,我深表歉意。我正在尝试在Robert Sedgewick和Kevin Wayne的《算法》第4版书中给出的Eclipse中运行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中将输入参数传递到哪里。任何帮助将不胜感激。
答案 0 :(得分:2)
答案 1 :(得分:1)
谢谢大家! 我解决了显然,该书网站上提供的较新的图书馆与我的书有所不同。
我将主要功能更改如下:
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)
{
In i = new In(args[0]);
In j = new In(args[1]);
int[] whitelist = i.readAllInts();
int[] iplist = j.readAllInts();
Arrays.sort(whitelist);
for (int key:iplist)
{ // Read key, print if not in whitelist.
if (rank(key, whitelist) < 0)
StdOut.println(key);
}
}
}
然后通过
"tinyW.txt" "tinyT.txt"
作为程序参数,如@ GDG612所示。
答案 2 :(得分:0)
右键单击您的程序,然后选择Run As -> Run Configurations...
然后单击(x)= Arguments
标签,并将输入传递到Program arguments