2017-08-04 84 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什么都不做。

相关问题