数组 indexoutofboundsException:3

Array indexoutofboundsException:3

无法弄清楚为什么我的 int[][] totalOpenness = new int[n][n]; 数组不断越界 exception:3。我的 public static void openfactor(char[][] mazeValue, int n) 方法的计算工作正常;但是,我在 openfactor(char[][] mazeValue, int n) 中的以下两行中收到数组索引越界错误,我似乎永远无法通过我的整个数组,甚至无法关闭。请帮忙! :)

totalOpenness[i][j] = openness;
System.out.println("TOTAL OPENESS FOR : [" + i + "]" +"[" + j + "]  IS " +totalOpenness[i][j]);

import java.util.*;
public class Game {

    public static void main(String[] args) {
    Scanner kbd = new Scanner(System.in);
    System.out.println("ENTER A SINGLE INTEGER: ");
    int n = kbd.nextInt();
    char[][] mazeValue = new char[n + 1][n + 1];
    System.out.println("ENTER A PATH: ");
    for (int i = 0; i < mazeValue.length; i++) {
        for (int j = 0; j < mazeValue[i].length; j++) {
            if (i == 0 || j == 0 || i == n + 1 || j == n + 1)
                mazeValue[i][j] = 'X';
            else {
                mazeValue[i][j] = kbd.next().charAt(0);
            }
        }
    }
    printMaze(mazeValue);
    openfactor(mazeValue, n);
}
public static void printMaze(char mazeValue[][]) {
    System.out.println("MAZE");
    for (int i = 1; i < mazeValue.length; i++) {
        for (int j = 1; j < mazeValue[i].length; j++) {
            System.out.printf("%5c", mazeValue[i][j]);
        }
        System.out.printf("\n");
    }
}
public static void openfactor(char[][] mazeValue, int n){

       int[][] totalOpenness = new int[n][n];
       for(int i = 1; i<=n; i++)
       {  
           
           for(int j=1;j<=n;j++)
          {

              int count=0;
              int openness=0;

               if(mazeValue[i][j]=='X'){
                   System.out.println("tHIS IS AN X FOR : [" + i + "]" +"[" + j + "] IS -1 ");
                   count--;
               }

              else 
               {
               //YOU NEED TO VERIFY THAT J IS NOT OUT OF BOUND
               if( j-1>=1)
                    {
               if(mazeValue[i][j-1]=='O')
                        count++;
                    }
                      // System.out.println("cout: "+count);

                    if(i-1>=1 && j-1>=1)
                    {
                    if(mazeValue[i-1][j-1]=='O')
                        count++;
                    }
                     //  System.out.println("cout: "+count);
                     if(i-1>=1)
                    {
                    if(mazeValue[i-1][j]=='O')
                        count++;
                     }
                    //   System.out.println("cout: "+count);
                    if(j+1<=n)
                    {
                    if(mazeValue[i][j+1]=='O')
                        count++;
                    }
                     //  System.out.println("cout: "+count);
                    if(j+1<=n && i+1<=n)
                    {
                    if(mazeValue[i+1][j+1]=='O')
                        count++;
                    }
                    if (i+1<=n)
                    {
                    if(mazeValue[i+1][j]=='O')
                        count++;
                    }
                    //   System.out.println("cout: "+count);
                    if(j-1>=1 && i+1<=n)
                    {
                    if(mazeValue[i+1][j-1]=='O')
                        count++;
                    }
                    if(i-1>=1 && j+1<=n)
                    {
                    if(mazeValue[i-1][j+1]=='O')
                        count++;
                    }
            //}//eND OF iF CONDITION\
            }
            openness = openness +count;
            totalOpenness[i][j] = openness;
            System.out.println("TOTAL OPENESS FOR : [" + i + "]" +"[" + j + "]  IS " +totalOpenness[i][j]);
           
         }
      }
   
   }

看看the documentation for ArrayIndexOutOfBoundsException:

Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.

您正在对 java 数组使用基于 1 的索引,而数组使用基于 0 的索引 (Java Tutorials):

for(int i = 1; i<=n; i++)
  for(int j=1;j<=n;j++)

将在 i=n 处给出异常,因为 totalOpenness[n][anynumber] 是一个无效的数组索引,这是因为它是用 int[][] totalOpenness = new int[n][n] 声明的,所以元素索引是 0, 1, ..., n-1

将循环更改为:

for(int i = 0; i < n; i++)
  for(int j = 0; j < n; j++)

不幸的是,for 循环中的逻辑似乎也假定从 1 开始索引,因此您也必须更改它。