2017-08-04 90 views
0

我寫了一個代碼,但我找不到輸出中顯示的錯誤。該程序輸入一些數字,我把它放在一個數組中。線程「主」java.lang.ArrayIndexOutOfBoundsException異常,數組數組

它有三個方法:

  1. 倒置的陣列;
  2. 找到最大值和最小值;
  3. 插入數字的平均值。

這裏是我的代碼:

package sequenza; 

import java.util.Scanner; 

public class SequenzaClass { 

    Scanner input = new Scanner(System.in); 
    int[] numeri; 
    int dim; 
    int max; 
    int min; 
    int media; 
    int somma; 
    int DIMENSIONE_MAX; 

    public SequenzaClass(int dim, int max, int min, int media, int somma, int DIMENSIONE) { 
     this.dim = dim; 
     this.max = max; 
     this.min = min; 
     this.media = media; 
     this.somma = somma; 
     this.DIMENSIONE_MAX = 10; 
    } 

    SequenzaClass() { 
     numeri = new int[DIMENSIONE_MAX]; 
     int dim = 0; 
     int max = 0; 
     int min = 0; 
     int media = 0; 
     int somma = 0; 
     int DIMENSIONE_MAX = 10; 
    } 

    public Scanner getInput() { 
     return input; 
    } 

    public void setInput(Scanner input) { 
     this.input = input; 
    } 

    public void Inserisci(int dim) { 
     System.out.print("How many numbers do you want to insert? "); 
     dim = input.nextInt(); 
     int i; 
     for (i = 0; i < dim; i++) { 
      System.out.print("Number for position [" + i + "]:"); 
      numeri[i] = input.nextInt(); 
     } 
    } 

    public int getDim() { 
     return dim; 
    } 

    public void setDim(int dim) { 
     this.dim = dim; 
    } 

    public int[] getNumeri() { 
     return numeri; 
    } 

    public void setNumeri(int[] numeri) { 
     this.numeri = numeri; 
    } 


    public int getMax() { 
     int max = numeri[0]; 
     int i; 
     for (i = 0; i < dim; i++) { 
      if (numeri[i] > max) { 
       max = numeri[i]; 
      } 
     } 
     return max; 
    } 

    public void setMax(int max) { 
     this.max = max; 
    } 

    public int getMin() { 
     int min = numeri[0]; 
     int i; 
     for (i = 0; i < dim; i++) { 
      if (numeri[i] < min) { 
       min = numeri[i]; 
      } 
     } 
     return min; 
    } 

    public void setMin(int min) { 
     this.min = min; 
    } 

    public int getSomma() { 
     int i; 
     for (i = 0; i < dim; i++) { 
      somma = somma + numeri[i]; 
     } 
     return somma; 
    } 

    public void setSomma(int somma) { 
     this.somma = somma; 
    } 

    public int getMedia() { 
     media = getSomma()/dim; 
     return media; 
    } 

    public void setMedia(int media) { 
     this.media = media; 
    } 

    public int getDIMENSIONE_MAX() { 
     return DIMENSIONE_MAX; 
    } 

    public void setDIMENSIONE_MAX(int DIMENSIONE_MAX) { 
     this.DIMENSIONE_MAX = DIMENSIONE_MAX; 
    } 



    public void Contrario(int dim) { 

     int i; 
     for (i = 0; i < (dim/2); i++) { 
      int k = numeri[dim - i - 1]; 
      numeri[dim - i - 1] = k; 
     } 
     System.out.println("The inverted vector is: " +numeri[i]); 

    } 

} 


package sequenza; 

import java.util.Scanner; 

public class main { 

    public static void main(String[] args) { 

     Scanner input = new Scanner(System.in); 
     int DIMENSIONE_MAX = 10; 
     int[] numeri = new int[DIMENSIONE_MAX]; 
     int dim = 0; 
     int max = 0; 
     int min = 0; 
     int media = 0; 
     int somma = 0; 
     int scelta = 1; 

     SequenzaClass sequenza1 = new SequenzaClass(); 

     sequenza1.Inserisci(dim); 

     while (scelta != 0) { 
      System.out.println("1) Invert numbers"); 
      System.out.println("2) Max and min"); 
      System.out.println("3) Avarage"); 
      scelta = input.nextInt(); 

      switch (scelta) { 
       case 1: 
        sequenza1.Contrario(dim); 
        break; 
       case 2: 
        System.out.println(sequenza1.getMax()); 
        System.out.println(sequenza1.getMin()); 
        break; 
       case 3: 
        System.out.println(sequenza1.getMedia()); 
        break; 
      } 

     } 
    } 

} 
+3

發佈完整的stacktrace,以便我們可以看到哪行代碼拋出異常。 – csmckelvey

回答

0

你需要做的檢查上所有方法訪問numeri,以確保指數小於DIMENSIONE_MAXnumeri.length如果你打算在運行時設置數組的大小。

public void Inserisci(int dim) { 
    System.out.print("How many numbers do you want to insert? "); 
    dim = input.nextInt(); 
    if (dim > DIMENSIONE_MAX || dim < 0) { 
     // handle this. it can't happen 
    } else { 
     int i; 
     for (i = 0; i < dim; i++) { 
      System.out.print("Number for position [" + i + "]:"); 
      numeri[i] = input.nextInt(); 
     } 
    } 
} 

另外請注意,這是一個非常糟糕的主意

public void setDIMENSIONE_MAX(int DIMENSIONE_MAX) { 
    this.DIMENSIONE_MAX = DIMENSIONE_MAX; 
} 

這會對你的數組的大小沒有影響,但是會造成我以前張貼以上失敗的檢查。數組的大小在創建時是固定的。因此,如果在創建數組之後調用此方法,那麼如果使用它作爲數組大小的指示器,則會發生不好的事情。如果使用DIMENSIONE_MAX作爲數組大小的指示符,那麼如果使用的是數組大小,它應該很可能被聲明爲不在函數中的類成員final定義數組的大小。

private static final int DIMENSIONE_MAX = 10; 

編輯 其實我只是看着你的代碼,並再次注意到您在3米不同的地點確定這個值。這需要修復,因爲這也是不好的做法。

本聲明即使什麼也不做:

SequenzaClass() { 
    numeri = new int[DIMENSIONE_MAX]; 
    int dim = 0; 
    int max = 0; 
    int min = 0; 
    int media = 0; 
    int somma = 0; 
    int DIMENSIONE_MAX = 10; 
} 

您的意思是:

SequenzaClass() { 
    numeri = new int[DIMENSIONE_MAX]; 
    this.dim = 0; 
    this.max = 0; 
    this.min = 0; 
    this.media = 0; 
    this.somma = 0; 
    this.DIMENSIONE_MAX = 10; 
} 

也請注意,如果上面的默認構造函數被調用DIMENSIONE_MAX還沒有確定,直到後面的陣列是創建。所以你基本上是創建一個大小爲0的數組,然後將LOCALDIMENSIONE_MAX設置爲10,它對於類成員DIMENSIONE_MAX什麼都不做。

相關問題