把字符串"3,1,2,4"以","分割拆分为数组,数组元素并按从小到大的顺序排列

时间:2022-02-14 21:04:43
package com.wangcf;
/**
* 把字符串"3,1,2,4"以","分割拆分为数组,数组元素并按从小到大的顺序排列
* @author fan
*
*/
public class Test{
/**
* 冒泡排序
* @param s
* @return
*/
public int[] paixu(int[] s){
for (int i=s.length; i >0; i--) {
int tem;
for (int j = 0; j < i-1; j++) {
if(s[j]>s[j+1]){
tem=s[j];
s[j]=s[j+1];
s[j+1]=tem;
}
}
}
return s;
} public static void main(String[] args) {
String s=new String("3,1,2,4");
String a[]=s.split(","); //以,隔开获得数组a[]={"3","1","2","4"}
int b[]=new int[a.length];
for(int i=0;i<a.length;i++){ //将String数组,转换为int数组
b[i]=Integer.parseInt(a[i]);
}
for(int c:b){
System.out.println(c);
} System.out.println("============================");
String s1=new String("3124");
String a1[]=s1.split(",");//由于s1中没有逗号,所以新生成的数组还是a1[]={"3124"}
System.out.println(a1.length);
System.out.println("---------------");
int b1[]=new int[s1.length()];
for (int i = 0; i < s1.length(); i++) { //将String数组转换为int数组
b1[i]=Integer.parseInt(String.valueOf(s1.charAt(i)));
}
for(int c1:b1){
System.out.println(c1);
}
System.out.println("+++++++++++++++++++");
Test t=new Test();
b=t.paixu(b);
for(int e:b){
System.out.println(e);
}
}
}

把字符串"3,1,2,4"以","分割拆分为数组,数组元素并按从小到大的顺序排列