Я пытаюсь сделать Bubble сортировать, и это мой код:
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)