-1
我想做一个程序,做一维细胞自动机。为此,我需要从一行读取三个变量。其中一个变量“L”决定“currentGeneration”的数组长度。但是,我得到ArrayIndexOut ...错误。我想这与我的阵列的尺寸和我不明白为什么我得到一个ArrayIndexOutOfBoundsException 2错误
public class Cellulitissss {
int L;
Scanner sc = new Scanner(System.in);
Boolean[] currentGeneration;
String automaton;
int G;
String X;
String Z;
public void readGeneral() {
String[] values = new String[2];
for (int i = 0; i < 3; i++) {
values[i] = sc.next();
}
automaton = values[0];
X = values[1];
Z = values[2];
L = Integer.parseInt(X);
G = Integer.parseInt(Z);
currentGeneration = new Boolean[L + 1];
}
}
因为'values [i]'仅针对'i = 0,1'(size = 2)存在 – nullpointer
没有索引'2' ,你的数组的索引为'0'和'1'。 – Berger
然后看看你的命名:诸如XLZG这样的名字意味着什么**都没有。使用单个大写字符作为* anything *的名称绝对没有理由。因此:阅读关于java命名约定,并开始练习。 – GhostCat