2012-11-27 70 views
0

我的代碼有問題。當我運行我的代碼時,eclipse告訴我我有arrayoutofboundexception錯誤。但我真的不知道我該如何解決這個問題。我讀了幾個與數組無關的異常出錯問題,但無法申請我的代碼。這是我的代碼與問題有關。謝謝。感謝你的幫助。在java程序中解決arrayoutofboundexception錯誤

char[][] cells; 
    int column = 300; 
    int row = 200; 
    cells= this.getCells(); 
    ... 
    if(cells[row][column] == '2'){ // error 
       // eat the pill 
       cells[row][column] = '1'; 
      } 
      else if(cells[row][column] == '3'){ 
       // eat the power pill 
       cells[row][column] = '1'; 
      } 


.... 

for (int r=0; r<rows; r++) { 
      for (int c=0; c<columns; c++) { 
       **if (cells[r][c] == '2')** { // error 
        //draw pill 
        g.fillRect(c*STEP-3, r*STEP-3, 6, 6); 


       } 
       else if (cells[r][c] == '3'){ 
        //draw power pill 
        g.fillRect(c*STEP-6, r*STEP-6, 12, 12); 

       } 
      } 
     } 


public char[][] getCells() 
    { 
     char[][] cells = new char[rows][columns]; 
     for (int r =0; r < rows; r++){ 
      System.arraycopy(lines.get(r).toCharArray(), 0, cells[r], 0, columns); 

回答

0

您試圖訪問數組的索引超出範圍的數組cells

後動初始化

cells= this.getCells(); 

檢查陣列的尺寸,您試圖訪問[200][300]這導致問題

+0

嘿,我添加charAt [] getCell方法,但我仍然不知道如何解決這個問題。 – JavaLeave

+0

嘗試在調試模式下運行你的代碼,並看到'單元格數組'的維數 –

+0

單元格[行] [列] ==>行是194,列是113,我認爲他們在200和300之內。我很迷惑 – JavaLeave

相關問題