【问题标题】:Array indexoutofboundsException:3数组 indexoutofboundsException:3
【发布时间】:2015-02-28 19:27:10
【问题描述】:

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

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]);
           
         }
      }
   
   }

【问题讨论】:

    标签: java arrays for-loop indexoutofboundsexception


    【解决方案1】:

    the documentation for ArrayIndexOutOfBoundsException

    抛出以指示已使用非法索引访问数组。索引为负数或大于或等于数组的大小

    您正在对 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 的索引,因此您也必须更改它。

    【讨论】:

    • 我使用 1 因为我在当前数组周围有一个 X 值边界。如果我从 0 开始,它只会存储我的寄宿生的一部分,不是吗?
    • 是的,它只是存储我的寄宿生。
    • 然后你需要将它声明为int[][] totalOpenness = new int[n+1][n+1],在这种情况下索引将是0, 1, ..., n。无论哪种方式,java 数组索引都是从 0 开始的,因此最大索引比长度小一。
    • 我这样做了,它确实有效,它保持在界限内,但这样做会导致它存储我的隐形寄宿生。我就是这样得到的,它仍然保留着隐形的寄宿生。
    猜你喜欢
    • 1970-01-01
    • 2017-01-13
    • 2012-04-26
    • 2020-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    相关资源
    最近更新 更多