java.lang.ArrayIndexOutOfBoundsException: 3 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23431065/
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-13 22:58:24  来源:igfitidea点击:

java.lang.ArrayIndexOutOfBoundsException: 3 error

javaindexoutofboundsexception

提问by chlc

I have the following piece of code for which i am getting java.lang.ArrayIndexOutOfBoundsException

我有以下一段代码 java.lang.ArrayIndexOutOfBoundsException

//divides ACode by 10 as many times as specified by the DecimalDigitPosition 
public static int getDecimalDigit(int ACode, int decimalDigitPosition) {
    if (decimalDigitPosition == 0)
        return ACode;
    else {
        ACode = ACode / (10 * decimalDigitPosition);
        int remainder = ACode % 10;
        return remainder;
    }
}

I am suspecting this line...

我怀疑这条线...

digit[i] = getDecimalDigit(samples[i].getValue(), 0);

in the code below which could be the reason for this exception.

在下面的代码中,这可能是此异常的原因。

//start a method to decode the message.
public static void decodeMessage(String filename) {
    Sound s = new Sound(filename);
    SoundSample[] samples = s.getSamples();
    int[] digit = new int[3];
    String message = "";
    int sampleIndex = 0;
    boolean nullReached = false;
    while (!nullReached) {
        int asciiValue = 0;
        for (int i = sampleIndex; i < sampleIndex + 3; i++) {
            if (i < samples.length) {
                // this line could be the cause of error..
                digit[i] = getDecimalDigit(samples[i].getValue(), 0);
                asciiValue += digit[i - sampleIndex] * (((i - sampleIndex) == 0) 
                ? 1 
                : ((i - sampleIndex) * 10));
            } else {
                for (int j = 0; j < 3; j++) {
                    digit[j] = 0;
                }
                break;
            }
        }
        if (digit[0] == digit[1] 
        && digit[1] == digit[2] 
        && digit[2] == 0) 
        nullReached = true;
        message += (char) asciiValue;
    }
}

Stack trace:

堆栈跟踪:

java.lang.ArrayIndexOutOfBoundsException: 3 at project.decodeMessage(project.java:107) at project.main(project.java:33) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav??a:25) at java.lang.reflect.Method.invoke(Method.java:597) at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:27??2) >

java.lang.ArrayIndexOutOfBoundsException: 3 at project.decodeMessage(project.java:107) at project.main(project.java:33) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke( NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav??a:25) at java.lang.reflect.Method.invoke(Method.java:597) at edu.rice.cs.drjava。 model.compiler.JavacCompiler.runCommand(JavacCompiler.java:27??2) >

采纳答案by Mike B

The problem is likely here in your decodeMessagemethod. I've snipped out some code to isolate the problem:

问题可能出在您的decodeMessage方法中。我剪掉了一些代码来隔离问题:

int[] digit = new int[3];
while (!nullReached)
{
    for (int i = sampleIndex; i < sampleIndex + 3; i++)
    {
        if (i < samples.length)
        {
            digit[i] = getDecimalDigit(samples[i].getValue(), 0);   // This should be the line that throws the exception                
        }               
    }           
    sampleIndex += 3;           
}

The second time through your whileloop, sampleIndexwill be 3, so iwill start at 3. Then you try to dereference digit[i], which would be digit[3]. Since digit[]is an int[3], then digit[3]is out of the bounds of the array.

第二次通过您的while循环,sampleIndex将是3,因此i将从 开始3。然后您尝试取消引用digit[i],这将是digit[3]. 由于digit[]是 an int[3],则digit[3]超出了数组的范围。

Maybe you want to do this instead:

也许你想这样做:

digit[i-sampleIndex] = getDecimalDigit(samples[i].getValue(), 0);