首页 > 编程语言 > 详细

需求:将字符串中的数值进行排序

时间:2017-02-06 19:50:03      阅读:246      评论:0      收藏:0      [点我收藏+]
import java.util.Iterator;
import java.util.TreeSet;
/*
需求:将字符串中的数值进行排序。
        例如String str="8 10 15 5 2 7"; ---->   "2 5 7 8 10 15"
*/

public class Demo8 {
    
    public static void main(String[] args) {
        String str="8 10 15 5 2 7";
        String[] datas = str.split(" ");
        
        TreeSet tree = new TreeSet();
        for(int i = 0 ; i<datas.length ; i++){
            tree.add(Integer.parseInt( datas[i])); // 字符串转int类型数据是需要使用Integer.parseInt()
        }
        
        //遍历treeSet的元素拼接成对应的字符串
        Iterator it = tree.iterator();
        while(it.hasNext()){
            System.out.print(it.next()+" ");
        }
        
    }

}

 

需求:将字符串中的数值进行排序

原文:http://www.cnblogs.com/xufengyuan/p/6371504.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!