首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解决java程序中arrayoutofboundexception错误

解决java程序中arrayoutofboundexception错误
EN

Stack Overflow用户
提问于 2012-11-27 09:41:12
回答 1查看 518关注 0票数 0

我的代码中有一个问题。当我运行我的代码时,eclipse告诉我有eclipse错误。但是我真的不知道如何为我的代码解决这个问题。我读了几个与数组越界异常错误相关的问题,但无法申请我的代码。这是我与问题相关的代码。谢谢。感谢你的帮助。

代码语言:javascript
复制
   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);
EN

回答 1

Stack Overflow用户

发布于 2012-11-27 09:42:09

您正在尝试访问超出数组cells范围的数组索引

初始化后

代码语言:javascript
复制
cells= this.getCells();

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13576170

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档