Java:ArrayIndexOutofBoundsException

Java:ArrayIndexOutofBoundsException,java,arrays,indexoutofboundsexception,Java,Arrays,Indexoutofboundsexception,这是我的arraylist方法,来自一个名为ants的类,它用于存储对象,如巢穴、食物和障碍物 public ArrayList<Cell> getObstacles() { ArrayList<Cell> obstacles = new ArrayList<Cell>(); for (int i = 0; i < columns; i++) { for (int j = 0; j < rows; j++

这是我的arraylist方法,来自一个名为ants的类,它用于存储对象,如巢穴、食物和障碍物

public ArrayList<Cell> getObstacles() {
    ArrayList<Cell> obstacles = new ArrayList<Cell>();
      for (int i = 0; i < columns; i++) {
            for (int j = 0; j < rows; j++) {
                Cell currentcell = cellArray[columns][rows];
              // Initialize each object
              if(currentcell.isBlocked()){
                  obstacles.add(currentcell);
              }
              else if(currentcell.isGoal()){
                  obstacles.add(currentcell);
              }
              else if(currentcell.hasNest()){
                  obstacles.add(currentcell);
              }
            }
          }
        return obstacles;
        }
public ArrayList getroblements(){
ArrayList障碍=新建ArrayList();
对于(int i=0;i
当我从另一个名为antscontrolpanel的类检索arraylist并在方法中使用它时,出现了一个错误arrayindexoutofboundexception。有人知道如何解决这个错误吗

public void savemap (File file)
      {
          ArrayList<Cell> obstacles = ants.getObstacles();

    if (file == null) {         
      if (chooser == null) chooser = createChooser();
      chooser.setDialogType(JFileChooser.SAVE_DIALOG);
      int r = chooser.showDialog(this, null);
      if (r != JFileChooser.APPROVE_OPTION) return;
      file = chooser.getSelectedFile();
    }

    /**
     * For obstacles added to the program:
     * Simple parsing of relevant data into txt file for easy reading.
     * Rows will be written like so:
     * 
     * 50 25 b
     * 51 25 b
     * 52 25 b
     * 34 28 n
     * .
     * .
     * 
     * Column, followed by row, followed by type of obstacle.
     * Assumption: Only one obstacle per cell. Should be a relatively simple extension for multiple obstacles per cell.
     */

    try {

        Writer writer = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(file), "utf-8"));
        for(int i = 0; i < obstacles.size(); i++)
        {
            Cell currentCell = obstacles.get(i);
            writer.write(Integer.toString(currentCell.c));
            writer.write(" ");
            writer.write(Integer.toString(currentCell.r));
            writer.write(" ");

            if(currentCell.isBlocked())
                writer.write(blockedChar);
            else if(currentCell.isGoal())
                writer.write(goalChar);
            else if(currentCell.hasNest())
                writer.write(nestChar);
            writer.write("\n");

        }

        writer.close();
    }

    catch (IOException e) {
      String msg = e.getMessage();
      this.stat.setText(msg); System.err.println(msg);
      JOptionPane.showMessageDialog(this, msg,
        "Error", JOptionPane.ERROR_MESSAGE);
    }                           /* set the status text */
    this.curr = file;          
  }  
public void保存映射(文件)
{
ArrayList障碍=ants.getBarriends();
如果(file==null){
如果(chooser==null)chooser=createChooser();
setDialogType(JFileChooser.SAVE_对话框);
int r=chooser.showDialog(此为空);
如果(r!=JFileChooser.APPROVE_选项)返回;
file=chooser.getSelectedFile();
}
/**
*对于添加到程序中的障碍:
*将相关数据简单解析为txt文件,便于阅读。
*行将按如下方式写入:
* 
*50 25 b
*51 25 b
*52 25 b
*34 28 n
* .
* .
* 
*列,后接行,后接障碍物类型。
*假设:每个单元只有一个障碍物。对于每个单元多个障碍物来说,应该是一个相对简单的扩展。
*/
试一试{
Writer Writer=new BufferedWriter(new OutputStreamWriter(
新文件输出流(文件),“utf-8”);
对于(int i=0;i

错误的索引。您应该使用
ray[i][j]
。这一行应该由错误消息明确指出。

看起来您的索引是
索引
,而不是
i
j

您需要提供堆栈跟踪,以便我们可以帮助您调试
Cell currentcell = cellArray[columns][rows];