[英]How to run Java programs from Algorithms textbook by Robert Sedgewick?
I apologize for a rookie question.对于菜鸟问题,我深表歉意。 I am trying to run the Java programs in Eclipse given in the Algorithms, 4th edition book by Robert Sedgewick and Kevin Wayne: https://algs4.cs.princeton.edu/home/我正在尝试运行 Eclipse 中的 Java 程序,该程序在 Robert Sedgewick 和 Kevin Wayne 的第 4 版书中给出: https://algs4.cs.princeton.edu/home/
I am having trouble with input arguments to the programs.我在向程序输入 arguments 时遇到问题。
For example for the following program:例如对于以下程序:
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);
}
}
}
The input arguments are:输入 arguments 是:
% java BinarySearch tinyW.txt < tinyT.txt
I don't know where to pass input arguments in Eclipse. Any help will be appreciated.我不知道在 Eclipse 中将输入 arguments 传递到哪里。任何帮助将不胜感激。
Thanks everyone! 感谢大家! I solved it. 我解决了 Apparently the newer libraries provided on the book site vary a bit from the book that I have. 显然,本书站点上提供的较新的图书馆与我所拥有的书籍有所不同。
I changed the main function as follows: 我将主要功能更改如下:
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);
}
}
}
Then pass 然后通过
"tinyW.txt" "tinyT.txt"
as Program arguments as indicated by @GDG612 . 作为程序参数,如@ GDG612所示。
右键单击您的程序,然后选择Run As -> Run Configurations...
然后单击(x)= Arguments
选项卡,并将输入传递到程序的“ Program arguments
you didn't have to change code, the library it is still work.您不必更改代码,它仍然可以使用的库。 but just how you compile is little bit different.但是你编译的方式有点不同。 for educational purposes i am put jar into src.出于教育目的,我将 jar 放入 src。
tools:工具:
vscode without plugin没有插件的vscode
jdk 17 jdk 17
compile:编译:
javac -cp./src/algs4.jar: BinarySearch.java javac -cp./src/algs4.jar: BinarySearch.java
java -cp./src/algs4.jar: BinarySearch tinyAllowlist.txt < tinyText.txt java -cp./src/algs4.jar: BinarySearch tinyAllowlist.txt < tinyText.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.