--------------------编程问答--------------------
public class Test1 {
public static String sort(String args) {
String[] temp = args.split(",");
int[] src = new int[temp.length];
for (int i = 0; i < temp.length; i++) {
src[i] = Integer.parseInt(temp[i]);
}
int[] srcD = bubbleSort(src);
String ans = "";
for (int i : srcD) {
ans += i + " ";
}
return ans;
}
public static int[] bubbleSort(int[] src) {//冒泡排序法,你也可以用其他排序方法来代替
for (int i = 1; i < src.length; i++) {
for (int j = 0; j < src.length - i; j++) {
if (src[j] > src[j + 1]) {
swap(src, j, j + 1);
}
}
}
return src;
}
public static void swap(int[] src, int i, int j) {//交换两个值
src[i] = src[i] + src[j];
src[j] = src[i] - src[j];
src[i] = src[i] - src[j];
}
public static void main(String[] args) {
String s = "100,10,1,0,15,20";
System.out.print(sort(s));// 输出0,1,10,15,20,100,
}
}
--------------------编程问答--------------------
public static void sort(String args){
String[] str = args.split(",");//分割字符串
//声明一个整形数组,用来存储分割后的字符串
int[] arr = new int[6];
for(int i = 0;i<str.length;i++){
arr[i] = (int)Integer.parseInt(str[i]);
}
//冒泡排序
for(int x =arr.length-1;x>0;x--){
for(int y = 0;y<x;y++){
int temp =0;
if(arr[y]>arr[y+1]){
temp = arr[y];
arr[y]=arr[y+1];
arr[y+1]=temp;
}
}
}
//循环便利整形数组
for(int i =0;i<arr.length;i++){
System.out.println(arr[i]);
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "100,10,1,0,15,20";
sort(s);
--------------------编程问答--------------------
public class Test1 {
public static String sort(String args){
StringBuffer value = "";
int[] nums = args.split(",");
nums = Arrays.sort(nums);
for(int i : nums) {
value.append(i + ",");
}
return value;
}
public static void main(String[] args) {
String s = "100,10,1,0,15,20";
System.out.print(sort(s));//输出0,1,10,15,20,100,
}
public class StringSort
{
public static void main(String[] args)
{
String s = "100,10,1,0,15,20";
System.out.print(sort(s));
}
public static String sort(String s)
{
char[] ch = s.toCharArray();
List<Integer> list1 = new ArrayList<Integer>();
List<Integer> list2 = new ArrayList<Integer>();
//获得所有","的位置
for (int i=0; i<s.length(); i++)
{
if (',' == ch[i])
{
list1.add(i);
}
}
int n = 0;
int m = 0;
Integer temp;
//获得字符串中的数字
for (int j=0; j<list1.size(); j++)
{
if (0 == j)
{
m = list1.get(j);
String str = new String(ch,n,m-n);
temp = new Integer(Integer.parseInt(str));
list2.add(temp);
n = m + 1;
}
else if (j>0 && j<list1.size()-1)
{
m = list1.get(j);
String str = new String(ch,n,m-n);
temp = new Integer(Integer.parseInt(str));
list2.add(temp);
n = m + 1;
}
else
{
m = list1.get(j);
String str = new String(ch,n,m-n);
temp = new Integer(Integer.parseInt(str));
list2.add(temp);
n = m + 1;
String last = new String(ch,n,ch.length-n);
Integer lastTemp = new Integer(Integer.parseInt(last));
list2.add(lastTemp);
}
}
//使用冒泡排序
for (int k=list2.size(); k>2; k--)
{
for (int q=0; q<k-1; q++)
{
if (list2.get(q) > list2.get(q+1))
{
int tem = list2.get(q);
list2.set(q, list2.get(q+1));
list2.set(q+1, tem);
}
}
}
StringBuffer strb = new StringBuffer();
for (int p=0; p<list2.size(); p++)
{
strb.append(list2.get(p));
if (list2.size()-1 != p)
{
strb.append(',');
}
}
String string = new String(strb);
return string;
}
}
--------------------编程问答--------------------
public class Test1 {
public static List<Integer> sort(String args){
String[] string=args.split(",");
List<Integer> list = new ArrayList<Integer>();
for (String string2 : string) {
list.add(Integer.parseInt(string2));
}
Collections.sort(list);
return list;
}
public static void main(String[] args) {
String s = "100,10,1,0,15,20";
System.out.print(sort(s));//输出0,1,10,15,20,100,
}
}
八楼是高手,这个还是看思维,这种题也就是练习题而已,实际项目中肯定不会是这样的,字符串里存这种内容,这个题的实际案例可以理解为典型的xml解析,或者是协议交互的参数解析等实际内容(比如以|分割的字符串等等)
--------------------编程问答--------------------