标签:class pre array builder pen 定义 lse dem build
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));
}
/*数组转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;
}
}
标签:class pre array builder pen 定义 lse dem build
原文地址:https://www.cnblogs.com/joker-dj/p/12983745.html