我正在浏览Sedgewick的算法书,我似乎无法使我的IDE运行他们的程序。程序启动但不会接受传递的参数。具体来说,我希望它打开tiny.txt文件,我在程序参数部分设置,但它只是被忽略...
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut;
public class Selection
{
public static void sort(Comparable[] a)
{ // Sort a[] into increasing order.
int N = a.length; // array length
for (int i = 0; i < N; i++)
{ // Exchange a[i] with smallest entry in a[i+1...N).
int min = i; // index of minimal entr.
for (int j = i+1; j < N; j++)
if (less(a[j], a[min])) min = j;
exch(a, i, min);
} }
// See page 245 for less(), exch(), isSorted(), and main().
private static boolean less(Comparable v, Comparable w)
{ return v.compareTo(w) < 0; }
private static void exch(Comparable[] a, int i, int j)
{ Comparable t = a[i]; a[i] = a[j]; a[j] = t; }
private static void show(Comparable[] a)
{ // Print the array, on a single line.
for (int i = 0; i < a.length; i++)
StdOut.print(a[i] + " ");
StdOut.println();
}
public static boolean isSorted(Comparable[] a)
{ // Test whether the array entries are in order.
for (int i = 1; i < a.length; i++)
if (less(a[i], a[i-1])) return false;
return true;
}
public static void main(String[ ] args)
{ // Read strings from standard input, sort them, and print.
String[] a = In.readStrings();
sort(a);
assert isSorted(a);
show(a);
}
}
答案 0 :(得分:1)
您似乎想要标准输入重定向。不幸的是,IntelliJ不支持这一点。
当您提供命令行参数时,所做的就是参数中的每个单词都作为args
方法的main
参数中的字符串传递。然后,您可以在main
中编写一些代码,以在该文件上打开BufferedReader。
另一种方法是打开终端窗口(或命令提示符窗口)并键入命令行java package.name.ClassName < filename.ext
。命令处理器或shell将<
字符解释为将标准输入重定向到提供的文件的请求。
答案 1 :(得分:1)
如果您想在mac OS Sierra上使用IntelliJ IDEA中的算法代码,这就是需要做的事情:
1,确保从他们的网站上获取并运行algs4.app
2,使用intelliJ终端导航到.class文件所在的algorithms2 / out / production / algorithms2
3,输入终端:$ java-algs4 Selection&lt; /Users/mistakeNot/Desktop/Java/algorithms2/tiny.txt