2014-10-28 84 views
2

我是java的新手。任何人都可以帮助我解决错误arrayindexoutofboundsexception如何修复java.lang.arrayIndexoutofboundsexception:0?

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

阵列的最大索引? – 2014-10-28 06:37:16

+0

我在线程“main”java.lang.ArrayIndexOutOfBoundsException中获取异常:0 \t at Minesweeper.main(Minesweeper.java:5)@Jens – user26 2014-10-28 06:38:07

+1

@ user26您的数组索引将从0开始,并将以M- 1或N-1 – Naveen 2014-10-28 06:44:09

回答

4

你必须检查是否存在有两个参数与否,与这样的代码:

if(args.length > 2) 

此外,我认为这是更好地改变这样的线路:

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

这样的:大概

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

关于'for'循环,代码过度分配内存:'new boolean [M + 2] [N + 2];',因此虽然不好的做法,但在这种情况下不是错误。 – 2014-10-28 06:41:59

+0

@ KenY-N是的你是对的 – Lrrr 2014-10-28 06:42:49

8

两件事情,因为它说指数0(请参阅同第一种情况):

  1. 你没有传递参数给你的主类。

  2. 数组索引总是从0开始,而不是1,所以,你可能需要改变之一:

    • 你的循环由0开始,检查我< M或N
    • 您使用数组索引我 - 1而不是我。你得到ArrayIndexOutOfBoundException

原因是可以说你有一个数组2个元素。它将如下:

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

而当你使用i = 1循环;我< = 2要访问:

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

其他人都忘记了传递参数:) – ADTC 2014-10-28 09:52:43

+0

我没有和[这是我的回答](http://stackoverflow.com/a/26618493/3128926):)我甚至把屏幕捕获eclipse以了解如何使用命令行参数和运行配置。 – 2014-10-29 00:38:50

4

你必须访问一个元件之前检查阵列args的长度。既然你有数组中访问第二个元素,在lengh应该ATLEAST 3.你必须检查类似下面

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

我宁愿使用防御性简短的'if(args.length <3)',并在里面放置错误消息和'return;'。包装整个方法体是IMO过度缩进。 – Joffrey 2014-10-28 06:45:57

3

(方案#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)

代替改变您的代码,则可以通过其代表INT M,N和双P 3命令行参数。要做到这一点在eclipse ide;

  1. 右键单击您的班级并选择run as - > run configurations;

enter image description here

  • 在他们之间选择的参数选项卡和写三个参数离开空间;
  • enter image description here

    现在,你不会得到 “ArrayIndexOutOfBoundsException异常” 再次,与值2 4 0.9,输出将是;

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

    (解释)

    有你的代码的两个问题;

    1. 你有ArrayIndexOutOfBoundsException异常例外,这意味着你的代码试图用超过指数达到一个数组值。

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

      异常线程 “main” java.lang.ArrayIndexOutOfBoundsException:0 在com.stack1.Minesweeper.main(Minesweeper.java:8)

    为了明确这一点,首先我将解释什么is 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”,超过其正好通过当不存在这样的元件不使用索引0存储在“intArray.length”

    相关问题