- package cn.itcast.api.p2.wrapper.test;
- import java.util.Arrays;
- public class WrapperTest2 {
- private static final String SPACE = " ";
- /**
- * @param args
- */
- public static void main(String[] args) {
- /*
- * 综合练习:
- * "34 9 -7 12 67 25"要求对这个字符串中的数值进行从小到大的排序,
- /
-
-
- String str = "34 9 -7 12 67 25";
-
- str = sortStringNumber(str);//这是个什么方法?有什么用?
- System.out.println(str);
- }
- public static String sortStringNumber(String str) {
- String[] str_nums = toStringArray(str);
- int[] nums = toIntArray(str_nums);
- sortIntArray(nums);
- return arrayToString(nums);
- }
- private static String arrayToString(int[] nums) {
-
- StringBuilder sb = new StringBuilder();//这里的字符串缓冲区有什么用》???
- for (int i = 0; i < nums.length; i++) {
- if(i!=nums.length-1)
- sb.append(nums[i]+SPACE);
- else
- sb.append(nums[i]);
- }
-
- return sb.toString();
- }
- private static void sortIntArray(int[] nums) {
- Arrays.sort(nums);//这里的方法有什么用??
- }
- private static int[] toIntArray(String[] str_nums) {
-
- int[] arr = new int[str_nums.length];
-
-
- for (int i = 0; i < str_nums.length; i++) {
-
- arr[i] = Integer.parseInt(str_nums[i]);
- }
-
-
- return arr;
- }
- private static String[] toStringArray(String str) {
-
- return str.split(SPACE);
- }
-
-
- }
复制代码 求各位大神帮忙解决在代码中注释的问题,告诉小弟为什么!谢谢啦!
|