package Learning;
import java.util.Arrays;
/**
- Title:MaoPao
- Description: 使用冒泡法排序数组中的元素
- @author:KimiLee
-
@date: 2017年10月10日
*/
public class MaoPao {
public static void main(String[] args) {
int[] a = new int[] {2,58,32,24,76,98,43,1,18,66};
int t;System.out.println("排序前的数组为:"); //foreach 输出数组a /*for (int k : a) { System.out.print(k+",");
}*/
System.out.println(Arrays.toString(a)); //冒泡排序 升序排列 for(int j=1;j<a.length-1;j++) { for (int i = 1;i<a.length-1;i++) { if(a[i]>a[i+1]) { t=a[i]; a[i]=a[i+1]; a[i+1]=t; } } } System.out.println(); System.out.println("排序后的数组为:"); for(int i = 1;i<a.length;i++) { System.out.print(a[i]+" "); } System.out.println(); System.out.println(Arrays.toString(a)); for (int v : a) { System.out.print(v+",");
}
}
}
输出结果:
排序前的数组为:
[2, 58, 32, 24, 76, 98, 43, 1, 18, 66]
排序后的数组为:
1 18 24 32 43 58 66 76 98
[2, 1, 18, 24, 32, 43, 58, 66, 76, 98]
2,1,18,24,32,43,58,66,76,98,
代码及输出结果如上所示,试了好多遍,实在搞不懂为什么排序后,三种方式输出数组会有这样的区别,求解答~~~~~~~~~~~