- import java.util.Arrays;
- public class WrapperTest {
- private static final String SPACE_SEPARATOR = " ";
- public static void main(String[] args){
- String numStr = "20 79 9 -7 88 36 29";
- System.out.println(numStr);
- System.out.println(sortNumStr(numStr));
- }
- public static String sortNumStr(String numStr){
- //将字符串变成字符数组
- String[] str_arr = stringToArray(numStr);
- //将字符串数组变成int数组
- int[] num_arr = toIntArray(str_arr);
- //对int数组排序
- mySortArray(num_arr);
- //将排序后的int数组变成字符串
- String finalStr = arrayToString(num_arr);
- return finalStr;
- }
- public static String[] stringToArray(String numStr){
- String[] str_arr = numStr.split(SPACE_SEPARATOR);
- return str_arr;
- }
- public static int[] toIntArray(String[] str_arr){
- int[] num_arr = new int[str_arr.length];
- for(int i = 0; i < str_arr.length; i++){
- num_arr[i]=Integer.parseInt(str_arr[i]);
- }
- return num_arr;
- }
- public static void mySortArray(int[] num_arr){
- Arrays.sort(num_arr);
- }
- public static String arrayToString(int[] num_arr){
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < num_arr.length; i++){
- if(i != num_arr.length-1)
- sb.append(num_arr[i]+SPACE_SEPARATOR);
- else
- sb.append(num_arr[i]);
- }
- return sb.toString();
- }
- }
复制代码
|
|