gpt4 book ai didi

java - 排序问题..

转载 作者:行者123 更新时间:2023-12-01 18:39:40 24 4
gpt4 key购买 nike

您好,我是编程新手,目前的任务是创建一个与排序相关的程序。我不断收到错误

 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at sorting.Sorting.main(Sorting.java:17)
Java Result: 1

它指出错误所在的行是第 17 行,其中包含:

 if (ArrayOfInts[j] > ArrayOfInts[j + 1])

请帮忙..这就是整个事情。

public class Sorting{
public static void main(String[] args){
int[] ArrayOfInts = {42, 97, 3, 689, 14, 1076, 3000, 8, 632, 327, 976, 4215};
for(int i = ArrayOfInts.length; i >= 1; i--){
for(int j = 0; j < i; j--){
if (ArrayOfInts[j] > ArrayOfInts[j + 1]){
int temp = ArrayOfInts[j];
ArrayOfInts[j] = ArrayOfInts[j + 1];
ArrayOfInts[j - 1] = temp;

}
}
}
for(int i = 0; i < ArrayOfInts.length; i++){
System.out.println(ArrayOfInts[i] + " ");
}
}
}

最佳答案

j=0时,这一行是有问题的:

ArrayOfInts[j - 1] = temp;

当您尝试访问数组中的 -1 索引时。

关于java - 排序问题..,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20465303/

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