简体   繁体   English

遍历数组时出现ArrayIndexOutOfBoundsException

[英]ArrayIndexOutOfBoundsException when iterating through an array

public static int calcScore(char[] inputChars) {
    int gameScore = 0;
    for(int i = 0; i < inputChars.length; i++) {
        System.out.println(inputChars[i]);
        if(inputChars[i] == 'X') {
            gameScore += 10;
            gameScore += getPointsStrikeSpare(i, inputChars);
        } else if (inputChars[i] == '-') {
            gameScore += 0;
        } else if (inputChars[i] == '/') {
            gameScore += 10;
            gameScore += getPointsStrikeSpare(i, inputChars);
        } else {
            gameScore += Character.getNumericValue(inputChars[i]);
        }
    }
    return gameScore;
}

So my problem is that I want to itterate through the inputChars array (created using .toCharArray() ) and it works fine but it cannot process the last character. 所以我的问题是我想遍历inputChars数组(使用.toCharArray()创建),它工作正常,但无法处理最后一个字符。

Here is the content of the input variable: "X-/X5-8/9-X811-4/X". 这是输入变量的内容:“ X- / X5-8 / 9-X811-4 / X”。

inputChars is input.toCharArray(). inputChars是input.toCharArray()。

Here is the output when I run the code : 这是我运行代码时的输出:

X
-
/
X
5
-
8
/
9
-
X
8
1
1
-
4
/
X
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 18
    at betterCalculateScore.getPointsStrikeSpare(betterCalculateScore.java:37)
    at betterCalculateScore.calcScore(betterCalculateScore.java:20)
    at betterCalculateScore.main(betterCalculateScore.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

I have tried using i < inputChars.length - 1 but then it just doesn't count the last character. 我尝试使用i <inputChars.length-1,但是它只是不计算最后一个字符。

Here is the content of getPointsStrikeSpare : 这是getPointsStrikeSpare的内容:

    public static int getPointsStrikeSpare(int i, char[] inputChars) {
    int points = 0;

    if(inputChars[i] == 'X') {
        if(inputChars[i+1] == '-') {
            points += 0;
        } else if (inputChars[i+1]== '/') {
            points += 10;
        } else {
            points += Character.getNumericValue(inputChars[i+1]);
        }
        if(inputChars[i+2] == '-') {
            points += 0;
        } else if (inputChars[i+2]== '/') {
            points += 10;
        } else {
            points += Character.getNumericValue(inputChars[i+2]);
        }
    } else {
        if(inputChars[i+1] == '-') {
            points += 0;
        } else if (inputChars[i+1]== '/') {
            points += 10;
        } else {
            points += Character.getNumericValue(inputChars[i+1]);
        }
    }
    return points;
}

At various places in the public static int getPointsStrikeSpare(int i, char[] inputChars) method, elements are being accessed which do not belong to the array length boundary. public static int getPointsStrikeSpare(int i, char[] inputChars)方法的各个位置上,正在访问不属于数组长度边界的元素。 Consider the code blocks: 考虑一下代码块:

    if(inputChars[i+1] == '-') 

as well as 以及

    if(inputChars[i+2] == '-') {

In the case of i == inputChars.length - 1, both i+1 as well as i+2 exceed the array length, hence the java.lang.ArrayIndexOutOfBoundsException exception. 在i == inputChars.length-1的情况下, i+1i+2超过了数组长度,因此出现java.lang.ArrayIndexOutOfBoundsException异常。

In general, if the value of 'i' is equal to or less than the length of (array_size - 1), a[i] would be valid. 通常,如果“ i”的值等于或小于(array_size-1)的长度,则a [i]是有效的。

However, in this method, public static int getPointsStrikeSpare(int i, char[] inputChars) one can see that a[i+1] and a[i+2] end up getting queried in different code paths in the if-else structure. 但是,在这种方法中, public static int getPointsStrikeSpare(int i, char[] inputChars)可以看到a [i + 1]和a [i + 2]最终在if-else结构中的不同代码路径中被查询。 。 So, if your 'i' index already reached the end of the array, this is going to be Out of Bounds. 因此,如果您的“ i”索引已到达数组的末尾,则将超出范围。

Ex: X-/X5-8/9-X811-4/X The size of this is 18. So, the legal values to refer are from 0-17 ie, a[0] to a[17], since indexing in array starts from 0. Towards the end of the program, index counter 'i' becomes 17. 例如: X-/X5-8/9-X811-4/X的大小为18。因此,要引用的合法值为从0-17,即a [0]到a [17],因为索引在数组从0开始。在程序结束时,索引计数器'i'变为17。

if(inputChars[i] == 'X') {               // VALID
        if(inputChars[i+1] == '-') {     // INVALID
            points += 0;
        } else if (inputChars[i+1]== '/') {  // INVALID
            points += 10;
        } else {
            points += Character.getNumericValue(inputChars[i+1]); // INVALID
        }
        if(inputChars[i+2] == '-') { // INVALID

You get the general idea here. 您在这里得到了一般的想法。

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

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