方法1:采用String的split,验证代码如下:
1 import java.util.*;
2
3 public class TestSplit
4 {
5 public static void main(String[] args)
6 {
7 String s = new String("1,4,2,6,9");
8 String[] arrStr = s.split(",");
9 int[] arrInt = {0,0,0,0,0};
10
11 for(int i=0;i<arrStr.length;i++)
12 arrInt[i] = Integer.parseIn(arrStr[i]);
13
14 Arrays.sort(arrInt);
15
16 for(int i=0;i<arrInt.length;i++)
17 System.out.println(arrInt[i]);
18 }
19 }
方法2:采用StringTokenizer
1 import java.util.*;
2
3 public class TestStringTokenizer {
4 public static void main(String[] args) {
5 String s = new String("1,4,7,3,5");
6 //int length = s.length();
7 StringTokenizer str = new StringTokenizer(s, ",");
8 String[] arrStr = new String[str.countTokens()];
9 int k = 0;
10 while(str.hasMoreTokens())
11 {
12 arrStr[k] = str.nextToken();
13 k++;
14 }
15 int[] arrInt = new int[arrStr.length];
16 for(int i=0;i<arrInt.length;i++)
17 arrInt[i] = Integer.parseInt(arrStr[i]);
18 Arrays.sort(arrInt);
19 for(int i=0;i<arrInt.length;i++)
20 System.out.println(arrInt[i]);
21 }
22 }
posted on 2008-03-24 11:52
冰枫逸范 阅读(108)
评论(0) 编辑 收藏 所属分类:
J2SE学习历程