从Java中的数组中获取时出错

Sza*_*arf 1 java arrays indexoutofboundsexception

我写了那种代码

int[] count = new int[10];
int i = count.length;
int position = -1;
int num = kb.nextInt();

    while(i > 0)
    {
        if(count[i] == num)
        {
            position = i;
            break;
        }
         i--;
    }
Run Code Online (Sandbox Code Playgroud)

但我得到了java.lang.ArrayIndexOutOfBoundsException错误

意图是找到用户在数组中选择的最后一个数字.

izo*_*ica 5

你设定i = count.length;.数组在java中从0开始索引,因此count[count.length]超出范围.数组中的最后一个有效索引aa.length -1.