繁体   English   中英

线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException,带数字的数组

[英]Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException , arrays with numbers

我写了一个代码,但我找不到输出中显示的错误。 该程序输入一些我放入数组的数字。

它具有三种方法:

  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;
            }

        }
    }

}

如果计划在运行时设置数组的大小,则需要检查所有访问 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的数组,然后将LOCAL DIMENSIONE_MAX设置为10,这对类成员DIMENSIONE_MAX没有任何作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM