gpt4 book ai didi

java - 由于未知原因返回 ArrayIndexOutOfBoundsException 错误

转载 作者:行者123 更新时间:2023-12-01 09:16:00 25 4
gpt4 key购买 nike

我正在尝试创建一个基本的冒泡排序程序,但在某些时候该数组尝试引用第 11 个位置,尽管该数组有 10 长,而且我不确定它何时发生

int Last, i = 0, Temp;
int[] Numbers = new int[10];
String[] NumbersString = new String[10];
String initialString = TextBox.getText();
NumbersString = initialString.split(" ");
while(i<10){
Numbers[i] = Integer.parseInt(NumbersString[i]);
i = i + 1;
}
Last = 9;
Boolean Swapped = true;
while(Swapped = true) {
Swapped = false;
i = 0;
while(i < Last) {
if(Numbers[i] > Numbers[i+1]){
Temp = Numbers[i];
Numbers[i] = Numbers[i+1];
Numbers[i+1] = Temp;
Swapped = true;
}
i = i + 1;
}
Last = Last-1;
}
String Result = Numbers[0] + " " + Numbers[1] + " " + Numbers[2] + " " + Numbers[3] + " " + Numbers[4] + " " + Numbers[5] + " " + Numbers[6] + " " + Numbers[7] + " " + Numbers[8] + " " + Numbers[9];
ResultText.setText(Result);

最佳答案

改变

 while(Swapped = true) {

 while(Swapped == true) {

发生的情况是 Last 首先包装为 Negative,然后当它达到最小负数时,它包装为 Integer.MAX_VALUE,然后 i 将超过 9

关于java - 由于未知原因返回 ArrayIndexOutOfBoundsException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40542501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com