我在算法课上有一堂这样的课:
public class Counter {
private final String name;
private int count;
public Counter(String id) {
name = id;
}
public void increment(){
count++;
}
public int tally(){
return count;
}
public String toString(){
return count + " " + name;
}
public static void main(String[] args) {
Counter heads = new Counter("heads");
Counter tails = new Counter("tails");
heads.increment();
heads.increment();
tails.increment();
StdOut.println(heads + " " + tails);
StdOut.println(heads.tally() - tails.tally());
}
}
当我在 Eclipse 中运行它时它工作正常,但是当我在终端中调用它时:
java -cp '../jar/algs4.jar:../jar/stdlib.jar' 计数器
它返回
线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:0 在 Counter.main(Counter.java:79)
这是为什么?