【发布时间】:2012-03-30 15:43:06
【问题描述】:
我正在尝试进行冒泡排序,这是我的代码:
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double[] test = new double[5];
double t;
//Set random value to each of elements
for(int i = 0;i<test.length;i++){
test[i] = Math.round((100*Math.random()));
System.out.println(test[i]);
}
//Bubble Sort
for(int i = 0;i<test.length;i++){
for(int k = 0;k<test.length-1;k++){
int x = i+1;
if(test[i]>test[x]){
t = test[i];
test[i] = test[x];
test[x] = t;
}
}
}
}
}
然后我启动它,它会抛出一个错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Main.main(Main.java:24)
【问题讨论】:
标签: java sorting bubble-sort