Java 如何修复“ArrayIndexOutOfBoundsException”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18471059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:54:24  来源:igfitidea点击:

How to fix "ArrayIndexOutOfBoundsException"?

javaindexoutofboundsexception

提问by Ziemowit Wójcik

I have big trouble with this exception. I'm newbie to Java programming (and programming as well), so.. Why, when second class is returning two arrays exception appears? In addition, when only one array is returning (f.e "return jakob;"), everything works ok. First class:

我有这个例外的大麻烦。我是 Java 编程(以及编程)的新手,所以.. 为什么,当第二个类返回两个数组时会出现异常?此外,当只有一个数组返回时 (fe "return jakob;"),一切正常。第一课:

package mainpackage;

public class GraWojna {
int[] jakob = new int[24];
int[] gracz = new int[24];
public void Wojna() {

    System.out.println("tasowanie kart...");
    // kart w talii jest 48, bo 4* 12.. true?
    WojnaTasowanie tas = new WojnaTasowanie();

    tas.Tasowanie(jakob, gracz);
    int licznik = 0;
    while (licznik<23) {
        System.out.println("jakob:" + jakob[licznik]);
        licznik++;
    }


}

}

Second class:

第二类:

package mainpackage;

import java.util.Random;


public class WojnaTasowanie {


int[] Tasowanie (int jakob[], int gracz[]) {
    int jakb[] = new int[23];
    int grcz[] = new int[23];
     Random generator = new Random();
     int licznik = 0;
     int[] Pula = new int[11];

    while (licznik<11){

        Pula[licznik] = 4;
        licznik++;
     }   
    licznik = 0;

        int passa = 0;
        int passb = 0;
    while (licznik<22) {

        passa = 5;
        passb = 5;
        System.out.println("step ----");

        while (passa<8){
        int bekaxd = generator.nextInt(11);


        if (Pula[bekaxd]>0) {
            Pula[bekaxd]--;
            passa = 15;
            jakob[licznik] = bekaxd;
        }



        while (passb<8){
            bekaxd = generator.nextInt(11);
            gracz[licznik] = bekaxd;
            System.out.println("licznik:" + licznik + "gracz: " + gracz[licznik] + "pula" + Pula[gracz[licznik]]);
            if (Pula[gracz[licznik]]>0) {
                Pula[gracz[licznik]]--;
                passb = 15;
            }

        }
        licznik++;

        }


    }

    return Tasowanie(jakob, gracz);

}

}

Stack trace:

堆栈跟踪:

 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 25
at mainpackage.WojnaTasowanie.Tasowanie(WojnaTasowanie.java:51)
at mainpackage.WojnaTasowanie.Tasowanie(WojnaTasowanie.java:76)
at mainpackage.WojnaTasowanie.Tasowanie(WojnaTasowanie.java:76)
at mainpackage.GraWojna.Wojna(GraWojna.java:12)
at mainpackage.Main.main(Main.java:16)

Thank you in advance!

先感谢您!

采纳答案by azzurroverde

you have licznik++; inside the wrong loop, move it down 2 lines and out of the inner loop.

你有 licznik++;在错误的循环内,将其向下移动 2 行并移出内循环。

 while (passb<8){
            bekaxd = generator.nextInt(11);
            gracz[licznik] = bekaxd;
            System.out.println("licznik:" + licznik + "gracz: " + gracz[licznik] + "pula" + Pula[gracz[licznik]]);
            if (Pula[gracz[licznik]]>0) {
                Pula[gracz[licznik]]--;
                passb = 15;
            }

        }


        }
      licznik++;

回答by guisantogui

IndexOutOfBoundsExceptionoccours when you try to access a position in an Array that doesn't exists:

IndexOutOfBoundsException当您尝试访问不存在的 Array 中的位置时发生:

Eg.

例如。

int[] a = {7,12,5}

int ac = a[3] //Here index out of Exception

them you must to find where you are trying to access an invalid position.

他们必须找到您试图访问无效位置的位置。

回答by Wayne Weibel

The Pula array has 11 elements, but the indexes are 0 .. 10. The random generator is making the int 11, so when used here (or any place the random number is used as an index):

Pula 数组有 11 个元素,但索引为 0 .. 10。随机生成器使 int 为 11,因此在此处使用时(或使用随机数作为索引的任何地方):

if (Pula[bekaxd]>0) {

it throws the error.

它抛出错误。

回答by Sajal Dutta

ArrayIndexOutOfBoundsExceptionis thrown because-

抛出ArrayIndexOutOfBoundsException是因为-

Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.

抛出以指示已使用非法索引访问数组。索引为负数或大于或等于数组的大小。

I cannot debug your class right now, but here are some examples why a program would throw this exception-

我现在无法调试您的课程,但这里有一些示例,为什么程序会抛出此异常 -

Say you have-

说你有——

int[] integers = new int [2];

// This is fine since our array has two indexes which are 0 and 1 with length being 2    
integers[0] = 2;  

// However this not fine since our array has a length of 2. So there's no such index 2 so saying integers[2] would throw that error
integers[2] = 3;

// Likewise this is ok
System.out.println(integers[1]);

// But this will throe the same exception
System.out.println(integers[3]);

So, debug your program and find where you have such scenario in your program so you can fix them.

因此,调试您的程序并找到您的程序中出现此类情况的位置,以便您可以修复它们。

回答by jfrank

It's a little hard to follow, but here is what I think is going wrong. Inside your loop that starts while (licznik< 22), there is another loop that starts while (passa < 8). Inside that inner loop, you have licznik++. It's my guess that this inner loop runs too may times, and results in a value for licznikthat is greater than the size of the jakobarray.

这有点难以理解,但这是我认为出错的地方。在开始的循环中while (licznik< 22),还有另一个循环开始while (passa < 8)。在那个内部循环中,你有licznik++. 我的猜测是这个内部循环运行了很多次,并导致它的值licznik大于jakob数组的大小。