作者:曹彩节 | 来源:互联网 | 2023-10-16 18:10
刚开始crousera上学习<algorithmspart1>但对JAVA实在是不熟。********************************************
刚开始crousera上学习
但对JAVA实在是不熟。
/*************************************************************************
* Compilation: javac ThreeSum.java
* Execution: java ThreeSum input.txt
* Dependencies: In.java StdOut.java Stopwatch.java
* Data files: http://algs4.cs.princeton.edu/14analysis/1Kints.txt
* http://algs4.cs.princeton.edu/14analysis/2Kints.txt
* http://algs4.cs.princeton.edu/14analysis/4Kints.txt
* http://algs4.cs.princeton.edu/14analysis/8Kints.txt
* http://algs4.cs.princeton.edu/14analysis/16Kints.txt
* http://algs4.cs.princeton.edu/14analysis/32Kints.txt
* http://algs4.cs.princeton.edu/14analysis/1Mints.txt
*
* A program with cubic running time. Read in N integers
* and counts the number of triples that sum to exactly 0
* (ignoring integer overflow).
*
* % java ThreeSum 1Kints.txt
* 70
*
* % java ThreeSum 2Kints.txt
* 528
*
* % java ThreeSum 4Kints.txt
* 4039
*
*************************************************************************/
/**
* The ThreeSum class provides static methods for counting
* and printing the number of triples in an array of integers that sum to 0
* (ignoring integer overflow).
*
* This implementation uses a triply nested loop and takes proportional to N^3,
* where N is the number of integers.
*
* For additional documentation, see Section 1.4 of
* Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class ThreeSum {
/**
* Prints to standard output the (i, j, k) with i < j < k such that a[i] + a[j] + a[k] == 0.
* @param a the array of integers
*/
public static void printAll(int[] a) {
int N = a.length;
for (int i = 0; i < N; i++) {
for (int j = i+1; j < N; j++) {
for (int k = j+1; k < N; k++) {
if (a[i] + a[j] + a[k] == 0) {
StdOut.println(a[i] + " " + a[j] + " " + a[k]);
}
}
}
}
}
/**
* Returns the number of triples (i, j, k) with i < j < k such that a[i] + a[j] + a[k] == 0.
* @param a the array of integers
* @return the number of triples (i, j, k) with i < j < k such that a[i] + a[j] + a[k] == 0
*/
public static int count(int[] a) {
int N = a.length;
int cnt = 0;
for (int i = 0; i < N; i++) {
for (int j = i+1; j < N; j++) {
for (int k = j+1; k < N; k++) {
if (a[i] + a[j] + a[k] == 0) {
cnt++;
}
}
}
}
return cnt;
}
/**
* Reads in a sequence of integers from a file, specified as a command-line argument;
* counts the number of triples sum to exactly zero; prints out the time to perform
* the computation.
*/
public static void main(String[] args) {
In in = new In(args[0]);
int[] a = in.readAllInts();
Stopwatch timer = new Stopwatch();
int cnt = count(a);
StdOut.println("elapsed time = " + timer.elapsedTime());
StdOut.println(cnt);
}
}
这是从书上摘取的源代码。
直接运行会报错
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at ThreeSum.main(ThreeSum.java:87)
不知道如何配置txt文件作为参数。
因为JAVA水平太低,不管是命令行还是eclijpse配置文件都不会。
请各位前辈指教
3 个解决方案
public static void main(String[] args) {
In in = new In(args[0]); // args[0]
int[] a = in.readAllInts();
args[0] 程序执行时,必须给程序赋值
如果是eclipse运行,在程序参数部分配置args[0] 的值
如果是dos命令行,在程序名后跟上参数值
否则 main方法拿不到args[0],就报数组越界了
eclipse的话,java文件右键 ->Run as -> Run configurations -> Arguments -> Program arguments
填写你的参数,多个用空格隔开
第一个就是你的args 的args[0], 第二个就是args[1] 以此类推
然后 Apply -> Run 就运行了
然后main方法中String file = args[0];
拿到的file 就是你配置的文件路径
然后做你的操作就是了
这个百度下,有更详细的说明