2017-09-25 160 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]將導致數組索引超出範圍

相關問題