解决java程序中的arrayoutofboundexception错误

时间:2012-11-27 01:41:12

标签: java indexoutofboundsexception

我的代码中存在问题。当我运行我的代码时,eclipse告诉我我有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);

1 个答案:

答案 0 :(得分:0)

您正在尝试访问超出数组cells

范围的数组索引

启动后

cells= this.getCells();

检查数组的维度,您正在尝试访问导致问题的[200][300]

相关问题