2017-09-25 161 views
-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]; 
    } 
} 
+1

因为'values [i]'仅针对'i = 0,1'(size = 2)存在 – nullpointer

+0

没有索引'2' ,你的数组的索引为'0'和'1'。 – Berger

+0

然后看看你的命名:诸如XLZG这样的名字意味着什么**都没有。使用单个大写字符作为* anything *的名称绝对没有理由。因此:阅读关于java命名约定,并开始练习。 – GhostCat

回答

0

您试图访问值变量L.办[2],因为数组索引从0开始使阵列中存储2个值将存储这是错误的他们在位置a [0]和[1],如果a是您的数组名称。因此,尝试访问[2]将导致数组索引超出范围

相关问题