字符怎么串排序?
String s ="12,32,28,3,42,17,8";
怎么写一个排序,按从小到大的顺序进行排列?
------解决方案--------------------String s ="12,32,28,3,42,17,8";
String[] temp = s.split(",");
int[] result = new int[temp.length];
int i = 0;
for (String string : temp) {
result[i] = Integer.parseInt(string.trim());
i++;
}
Arrays.sort(result);
System.out.println(Arrays.toString(result));
//小心中英文的逗号。。。。
------解决方案--------------------
public static void sortDemo() {
String str = "12,32,28,3,42,17,8";
String[] sourceArray = str.split(",");
List<Integer> ls = new ArrayList<Integer>();
for (String item : sourceArray) {
ls.add(Integer.parseInt(item));
}
Integer[] resultArray = ls.toArray(new Integer[ls.size()]);
Arrays.sort(resultArray);
System.out.println(Arrays.toString(resultArray));
}
网上很多例子的你可以随便搜如果你要自己写冒泡,快排那更加多了~