简体   繁体   English

ArrayIndexOutOfBoundsException,同时将2D数组条目初始化为0

[英]ArrayIndexOutOfBoundsException while initializing 2D array entries to 0

I need to create a dynamic 2D array which the user can edit. 我需要创建一个用户可以编辑的动态2D数组。 I've tried many different ways and even tried to do it separately to easier diagnose, but always get a java.lang.ArrayIndexOutOfBoundsException . 我尝试了许多不同的方法,甚至尝试单独进行操作以简化诊断,但是始终会得到java.lang.ArrayIndexOutOfBoundsException Below is some code (not from my project) that shows the problem. 下面是一些代码(不是来自我的项目)来显示问题。 I get the error when I try to fill the board with 0 . 当我尝试用0填充木板时出现错误。

public class Example {
    public static void main (String args[]) {
        int rows = 0;
        int cols = 0;
        int[][] board = new int[rows][cols];
        Scanner scan = new Scanner (System.in);

        System.out.print("Enter in a row  :");
        rows = scan.nextInt();
        System.out.print("Enter in a col :");
        cols =scan.nextInt();

        for (int i = 0; i < rows; i++)  {
            for (int j = 0; j < cols; j++)  {
            board[i][j] = 0;
                    System.out.print ("\t" + board[i][j]);                       
                } 
            System.out.print ("\n"); 
            }
        }
    }

You're initializing the array with 0 rows and 0 columns. 您正在使用0行和0列初始化数组。 That's having place for nothing. 那是一无所有的地方。 If a user inputs 1 and 1 for rows and columns, you try to access the first row. 如果用户为行和列输入1和1,则尝试访问第一行。 But there is no row. 但是没有排。

You should initialize your board after getting the amount of rows and columns from the user. 从用户那里获得行和列的数量之后,您应该初始化您的电路板。

int rows = 0; // the Java default value for integers is 0. Equivalent: int rows;
int cols = 0; // equivalent: int cols;

Scanner scan = new Scanner (System.in);

System.out.print("Enter in a row  :");
rows = scan.nextInt();
System.out.print("Enter in a col :");
cols =scan.nextInt();

int[][] board = new int[rows][cols]; // now has values other than 0

for (int i = 0; i < rows; i++)
{
   for (int j = 0; j < cols; j++) 
   {
      board[i][j] = 0;
      System.out.print ("\t" + board[i][j]);                         
   } 
   System.out.print ("\n"); 
}

Ideally, you would want to verify user input to see that they give dimensions that make sense. 理想情况下,您需要验证用户输入,以确保他们给出了有意义的尺寸。

Of course you get an ArrayIndexOutOfBoundsException . 当然,您会得到ArrayIndexOutOfBoundsException You are initializing your array with [0][0] dimensions. 您正在使用[0][0]维初始化数组。 Take a closer look at what values rows and cols have. 仔细研究一下rowscols值。

Fix: 固定:

Allow a maximum of n rows and m cols. 最多允许n行和m列。
eg int rows = 5, cols = 6 例如int rows = 5, cols = 6

or just move your array initalization after you've read rows and cols from the Scanner . 或者只是移动你的阵列initalization您已经阅读后rowscolsScanner

Go nuts: 发疯:

int rows = Integer.MAX_VALUE;
int cols = Integer.MAX_VALUE;

If think, it should be something like this: 如果想想,应该是这样的:

public class Example {

    public static void main (String args[]) { 
        int rows = 0; 
        int cols = 0; 
        Scanner scan = new Scanner (System.in);

       System.out.print("Enter in a row  :");
       rows = scan.nextInt();
       System.out.print("Enter in a col :");
       cols = scan.nextInt();

       int[][] board = new int[rows][cols]; 

       for (int i = 0; i < rows; i++)
       {
           for (int j = 0; j < cols; j++) 
           {
               board[i][j] = 0;
               System.out.print ("\t" + board[i][j]);                         
           } 
           System.out.print ("\n"); 
       }
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM