数字字符串排序后重新输出

package com.Demo.regex;


import java.util.Arrays;


public class Test1 { //字符串排序后重新输出


public static void main(String[] args) {


String s = "91 27 46 38 50";
String sarr[] = s.split(" ");
int arr[] = new int[sarr.length];
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(sarr[i]);
}


Arrays.sort(arr);
// System.out.println(Arrays.toString(arr));


/*  //字符串处理,浪费内存
* String st = ""; for (int i = 0; i < arr.length; i++) { if (i == arr.length) {
* st += arr[i]; } else {

* st += arr[i] + " "; }

* } System.out.println(st);
*/
/*//StringBuilder处理,节约内存
* StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++)
* { if (i == arr.length) {

* sb.append(arr[i]); } else { sb.append(arr[i] + " ");

* }

* } System.out.println(sb);
*/
//正则式方式,简单快捷
s = Arrays.toString(arr);
String regex = ",";
String regex1 = "\\[|\\]";


s = s.replaceAll(regex, " ").replaceAll(regex1, "");


//s = s.replaceAll(regex, " ");
System.out.println(s);


}


}
阅读更多
文章标签: java
个人分类: java.regex
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭