代码:
代码语言:javascript复制public class demo {
public static void main(String[] args) {
//定义一个数组
int[] arr = {12, 15, 10, 18, 11};
System.out.println("排序前" ArrayToString(arr));
/*冒泡排序*/
for (int i = 0; i < arr.length-1; i ) {
for (int j = 0; j < arr.length-1-i; j ) {
if (arr[j]>arr[j 1]){
int temp=arr[j];
arr[j]=arr[j 1];
arr[j 1]=temp;
}
}
}
System.out.println("排序后" ArrayToString(arr));
/*Arrays排序*/
Arrays.sort(arr);
System.out.println("排序后" ArrayToString(arr));
}
/*数组转String*/
public static String ArrayToString(int[] arr){
StringBuilder sb=new StringBuilder();
sb.append("[");
for (int i = 0; i < arr.length; i ) {
if(i==arr.length-1){
sb.append(arr[i]);
}else{
sb.append(arr[i]).append(",");
}
}
sb.append("]");
String s=sb.toString();
return s;
}
}