我已经实现了一个应该使用运行长度编码从标准输入压缩或扩展二进制输入的类。我已经修复了我的 IDE 标记的所有错误,但是当我实际运行它时,我得到了一个错误。
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at runlength.RunLength.main(RunLength.java:49)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
在代码中,第 49 行如下:
switch (args[0]) {
here is the full code:
package runlength;
import edu.princeton.cs.introcs.BinaryStdIn;
import edu.princeton.cs.introcs.BinaryStdOut;
public class RunLength {
private static final int R = 256;
private static final int lgR = 8;
public static void expand() {
boolean b = false;
while (!BinaryStdIn.isEmpty()) {
int run = BinaryStdIn.readInt(lgR);
for (int i = 0; i < run; i++)
BinaryStdOut.write(b);
b = !b;
}
BinaryStdOut.close();
}
public static void compress() {
char run = 0;
boolean old = false;
while (!BinaryStdIn.isEmpty()) {
boolean b = BinaryStdIn.readBoolean();
if (b != old) {
BinaryStdOut.write(run, lgR);
run = 1;
old = !old;
}
else {
if (run == R-1) {
BinaryStdOut.write(run, lgR);
run = 0;
BinaryStdOut.write(run, lgR);
}
run++;
}
}
BinaryStdOut.write(run, lgR);
BinaryStdOut.close();
}
public static void main(String[] args) {
switch (args[0]) {
case "-":
compress();
break;
case "+":
expand();
break;
default:
throw new IllegalArgumentException("Illegal command line argument");
}
}
}
如果有人可以向我解释我的问题是什么,我将不胜感激。