今日、クラスでバブルソートのチュートリアルがあり、修正方法がわからないというエラーが発生しました。
スレッド"main"の例外java.lang.ArrayIndexOutOfBoundsException:8 at BubbleSorter.main(BubbleSorter.java:24)
評価はされていませんが、これからも続けていきたいと思います。ありがとうございました。以下は私のコード全体です。
public class BubbleSorter {
public static void main(String[] args)
{
int i;
int array[] = { 12, 9, 4, 99, 120, 1, 3, 10 };
System.out.println("Array Values before the sort:\n");
for (i = 0; i < array.length; i++)
System.out.print(array[i] + " ");
System.out.println();
System.out.println();
bubble_srt(array, array.length);
System.out.print("Array Values after the sort:\n");
for (i = 0; i < array.length; i++)
;
System.out.print(array[i] + " ");
System.out.println();
System.out.println("PAUSE");
}
private static void bubble_srt(int[] array, int length) {
int i, j, t = 0;
for (i = 0; i < length; i++) {
for (j = 1; j < (length - 1); j++) {
if (array[j - 1] > array[j]) {
t = array[j - 1];
array[j - 1] = array[j];
array[j] = t;
}
}
}
}
}