class ArrayTest02
{
public static void main(String[] args)
{
int[] arr = new int[]{0,7,18,5,6,9,8,4,10,12,14,16};
printarr1(arr);
bubbleSort(arr);
printarr1(arr);
}
public static void printarr1(int[] arr)
{
System.out.print("[");
for (int x = 0 ;x < arr.length ;x++ )
{
if (x!=arr.length-1)
{
System.out.print(arr[x]+",");
}
else
System.out.println(arr[x]+"]");
}
}
public static void bubbleSort(int[] arr)
{
for (int x = 0;x <arr.length-1 ;x++ )
{
for (int y = 0;y<arr.length-x ;y++ )
{
if (arr[y]>arr[y+1])
{
int temp = arr[y];
arr[y]=arr[y+1];
arr[y+1]=temp;
}
}
}
}
}
出现Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12
at ArrayTest02.bubbleSort(ArrayTest02.java:30)
at ArrayTest02.main(ArrayTest02.java:7) |