package test1;public class BubbleSort {
static String sortArray(int before[]){
String result="";
for(int i=0;i<before.length;i++){
result+=before[i]+" ";
}
return result;
}
static int[] bubbleSort(int before[]){
int t;
boolean b=true;
for(int i=before.length-1;i>0&&b==true;i++){
b=false;
for(int j=0;j<i;j++){
if(before[j]>before[j+1]){
t=before[j];
before[j]=before[j+1];
before[j+1]=t;
b=true;
}
}

}
return before;
}
public static void main(String args[]){
int a[]={12,43,23,56,8,22,65,87};
System.out.println("Before Sorting:"+sortArray(a));
a=bubbleSort(a);
System.out.println("After Sorting:"+sortArray(a));
}
}
运行报错:
Before Sorting:12 43 23 56 8 22 65 87 
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
at test1.BubbleSort.bubbleSort(BubbleSort.java:17)
at test1.BubbleSort.main(BubbleSort.java:31)