我不明白为什么我会收到 ArrayIndexOutOfBoundsException 2 错误

I dont understand why i get an ArrayIndexOutOfBoundsException 2 error

我正在尝试制作一个在一维中执行元胞自动机的程序。为此,我需要从一行中读取三个变量。其中一个变量 "L" 决定了 "currentGeneration" 的数组长度。但是我得到 ArrayIndexOut... 错误。我认为这与我的数组的维度和变量 L 有关。

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];
    }
}

您尝试访问值 [2],这是错误的,因为数组索引从 0 开始,因此存储 2 个值的数组会将它们存储在位置 a[0] 和 a[1](如果 a 是您的数组名称)。因此尝试访问 a[2] 将给出数组索引越界异常