2014-10-28 82 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」

    相關問題