如何修复java.lang.arrayindexoutofboundsexception:0?

我是java的新手。 任何人都可以帮我解决错误arrayindexoutofboundsexception

 public class Minesweeper { public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); double p = Double.parseDouble(args[2]); // game grid is [1..M][1..N], border is used to handle boundary cases boolean[][] bombs = new boolean[M+2][N+2]; for (int i = 1; i <= M; i++) for (int j = 1; j <= N; j++) bombs[i][j] = (Math.random() < p); // print game for (int i = 1; i <= M; i++) { for (int j = 1; j <= N; j++) if (bombs[i][j]) System.out.print("* "); else System.out.print(". "); System.out.println(); } // sol[i][j] = # bombs adjacent to cell (i, j) int[][] sol = new int[M+2][N+2]; for (int i = 1; i <= M; i++) for (int j = 1; j <= N; j++) // (ii, jj) indexes neighboring cells for (int ii = i - 1; ii <= i + 1; ii++) for (int jj = j - 1; jj <= j + 1; jj++) if (bombs[ii][jj]) sol[i][j]++; // print solution System.out.println(); for (int i = 1; i <= M; i++) { for (int j = 1; j <= N; j++) if (bombs[i][j]) System.out.print("* "); else System.out.print(sol[i][j] + " "); System.out.println(); } } } 

这是例外:

 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Minesweeper.main(Minesweeper.java:5) 

可能有两件事,因为它表示索引0(参见第一个案例):

  1. 您没有将参数传递给主类。

  2. 数组索引始终从0开始而不是1.因此您可能需要更改:

    • 你的循环从0开始并检查i
    • 您使用数组索引作为i – 1而不是i。

您获得ArrayIndexOutOfBoundException的原因是假设您在数组中有2个元素。 它将如下:

 a[0] = 1; a[1] = 2; 

当你使用i = 1循环时; 我<= 2你正在访问:

 a[1] - which is perfect a[2] - which was not expected? 

在访问元素之前,您必须检查数组args的长度。 因为您必须访问数组中的第二个元素,所以长度应至少为3.您必须检查如下所示

 if(args.length > 2) { //wrap all code here } 

你必须检查是否有两个args,代码如下:

 if(args.length > 2) 

另外我认为更改这样的行更好:

 for (int i = 1; i <= M; i++) { for (int j = 1; j <= N; j++) 

对此:

 for (int i = 0; i  

(解决方案#1)

你已经使用了如下的命令行参数,args [0],args 1和args [3]意味着你的类需要3个参数才能正确运行,但是你没有提供任何参数,

 // int M = Integer.parseInt(args[0]); // int N = Integer.parseInt(args[1]); // double p = Double.parseDouble(args[2]); int M = 2; int N = 3; double p = 3.99; 

然后你的代码输出将是;

 * * * * * * * * * * * * 

您的代码需要3个参数,我上面所做的只是使用赋值而不是使用args []数组的值来分配这些参数。

(解决方案#2)

您可以传递3个命令行参数,而不是更改代码,这些参数代表int M,N和double p。 在eclipse ide中这样做;

  1. 右键单击您的类并选择run as – > run configurations;

在此处输入图像描述

  1. 选择参数选项卡并编写三个参数,在它们之间留出空间;

在此处输入图像描述

现在你不会再次获得“ ArrayIndexOutOfBoundsException ”,值为2 4 0.9,输出将是;

 * * . * * . * * * * 4 * * 4 * * 

(说明)

你的代码有两个问题;

  1. 您有ArrayIndexOutOfBoundsExceptionexception,这意味着您的代码尝试使用超过索引的数组值。

  2. 您正在使用命令行参数,但您没有输入任何参数作为参数。 您的主要方法需要3个参数才能正确运行但没有参数。 args[0]args[1]args[2]意味着有一个带有args []的数组名称,并且你试图到达args []数组的第0,第1和第2个元素。 但是,如果您不提供命令行参数,那么您将尝试达到空值,从而获得;

    线程“main”中的exceptionjava.lang.ArrayIndexOutOfBoundsException:0,位于com.stack1.Minesweeper.main(Minesweeper.java:8)

为了说清楚,首先我将解释什么是ArrayIndexOutOfBoundsException ;

当代码中的语句尝试访问索引大于数组长度的数组索引时,会发生ArrayIndexOutOfBoundsException错误。 让我解释一下;

假设你有2辆车。 第一辆车的颜色是红色第二辆车的颜色是蓝色 。 如果我问你“你的第二辆车的颜色是什么”,你会给我答案为蓝色。 但是我问的是“你的第五辆车的颜色是什么?” 你会说“我没有第五辆车”。

ArrayIndexOutOfBoundsException ”错误是相同的,在这种情况下,您只返回“ ArrayIndexOutOfBoundsException ”错误,因为索引5大于您的汽车索引的最大计数,实际上是汽车数组的长度。

 String car[] = new String[2]; car[0] = "blue"; car[1] = "red"; 

为了说清楚,让我们运行下面的代码;

 public class CarColorExample { public static void main(String[] args) { String[] carArray = new String[2]; carArray[0] = "blue"; carArray[1] = "red"; System.out.println("1st car color value: " + carArray[0]); System.out.println("2nd car color value: " + carArray[1]); System.out.println("3rd car color value: " + carArray[2]); } } 

如果您尝试运行上面的代码,您将获得如下例外;

 1st car color value: blue 2nd car color value: red Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at com.stack1.CarColorExample.main(CarColorExample.java:15) 

有一个语句“java.lang.ArrayIndexOutOfBoundsException:2” ,实际上指向超越索引告诉你carArray没有第二个索引。

 index: 0 --> "blue" index: 1 --> "red" index: 2 --> ???? 

作为关于ArrayIndexOutOfBoundsException错误的第二个例子,只需查看下面的代码并运行它;

  public static void main(String[] args) { int[] intArray = new int[3]; intArray[0] = 25; intArray[1] = 36; intArray[2] = 99; //Will work with no error for(int i = 0; i < intArray.length; i++) System.out.println("index[" + i + "], value:" + intArray[i]); // Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 // at com.stack1.ErrorExample2.main(ErrorExample2.java:18) // // "ArrayIndexOutOfBoundsException" error will occur for index: 3 // The length of the array is 2 // index: 0 --> 25 // index: 1 --> 36 // index: 2 --> 99 // There is no third index, That's all for(int i = 0; i < intArray.length + 1; i++) System.out.println("index[" + i + "], value:" + intArray[i]); } } 

同样的问题,索引'3'超出了数组的最大索引,该索引完全存储在“intArray.length”中