Dar*_*ren 0 java arrays exception bubble-sort
我今天在课堂上有一个泡泡排序的教程,我有一个错误,我不知道如何解决.
线程"main"中的异常java.lang.ArrayIndexOutOfBoundsException:8在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;
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
你有一个小错误:
这个:
for (i = 0; i<array.length; i++);
System.out.print(array[i] + " ");
Run Code Online (Sandbox Code Playgroud)
应该:
// v - Semicolon removed
for (i = 0; i<array.length; i++)
System.out.print(array[i] + " ");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1030 次 |
| 最近记录: |