0
我已經實現了一個應該使用運行長度編碼來壓縮或擴展來自標準輸入的二進制輸入的類。我修復了由IDE標記的所有錯誤,但是當我真正運行它時,出現錯誤。Java:運行長度編碼實現
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");
}
}
}
如果誰能給我解釋一下我的問題是什麼,我真的很感激它。
你傳遞任何參數傳遞給主? –