代码是冒泡排序,但不知道为什么会报错
import java.util.Scanner;
public class Sort {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int n=input.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++) {
a[i]=input.nextInt();
}
for(int i=0;i<n-1;i++) {
for(int j=i;j<n;j++) {
if(a[j]>a[j+1]) {
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
for(int i=0;i<n;i++) {
System.out.print(a[i]+" ");
}
System.out.println();
}
}
错误信息:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 6 out of bounds for length 6
at Sort.main(Sort.java:12)