package com.company;
public class BubbleSort {
public static void main(String[] args){
float[] array = new float[] {18, 2, 21, 3, 9};
float swap;
for (int i = 0; i < 5; i++){
if(array[i] > array[i + 1]){
swap = array[i];
array[i] = array[i + 1];
array[i + 1] = swap;
}
else {
continue;
}
}
for (int x = 0; x < 5; x++)
System.out.print(array[x] + " ");
System.out.print("");
}
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
В чем проблема?
for (int i = 0; i < 5; i++)
=>for (int i = 0; i < 4; i++)