A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wufengqiao 中级黑马   /  2016-4-23 23:47  /  1926 人查看  /  10 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

分析以下需求,并用代码实现:
        (1)有一个数字字符串"25 11 -6 20 102 9",数字之间用空格间隔       
        (2)利用冒泡排序对这串数字按照从小到大排序,生成一个数值有序的字符串"-6 9 11 20 25 102"
  要求:不是使用split方法

10 个回复

倒序浏览
                int a[]={25 ,11, -6, 20, 102, 9};               int temp=0;            
  for(int i=0;i<a.length-1;i++){              
     for(int j=0;j<a.length-1-i;j++){         
         if(a[j]>a[j+1]){            
          temp=a[j];            
           a[j]=a[j+1];           
            a[j+1]=temp;      
            }
                  }            
   }   
           for(int i=0;i<a.length;i++)        
           System.out.println(a);
回复 使用道具 举报
是的,split生成数组,然后排序即可.
回复 使用道具 举报
不用split,怎么玩啊?
回复 使用道具 举报
nc11111 发表于 2016-4-24 00:32
int a[]={25 ,11, -6, 20, 102, 9};               int temp=0;            
  for(int i ...

没有split语句啊
回复 使用道具 举报
如果不用spilt那就用死办法,substring截取
回复 使用道具 举报
byte[] getBytes()    字符串的这个返回数组的方法应该可以试试
回复 使用道具 举报
用字符串的indexOf()和subtring()结合就可以获取字符串里面的每一个数字,把获取的数字添加到数组或者集合中.
回复 使用道具 举报
先切割空格把返回值赋值给一个int数组 然后用冒泡排序 大该思路就这个这样
回复 使用道具 举报
String str = "25 11 -6 20 102 9";
回复 使用道具 举报
不是很难的,自己排序吧
     public static void main(String[] args) {
                String str = "25 11 -6 20 102 9";
                ArrayList<String> list = new ArrayList<String>();
                while(true) {
                        if(!str.contains(" ")) {
                                list.add(str);
                                break;
                        }
                        int index = str.indexOf(" ");
                        list.add(str.substring(0,index));
                        str = str.substring(index + 1);
                }
        }
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马