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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 聖手`书生 中级黑马   /  2013-4-16 19:02  /  1254 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 聖手`书生 于 2013-4-17 09:04 编辑
  1. //需求:对字符串中字符进行自然顺序排序。

  2. class  StringTest4
  3. {
  4.         public static String reverseString(String s)
  5.         {
  6.                 //字符串变数组
  7.                 char[] chs = s.toCharArray();
  8.                
  9.                 //对数组按自然顺序排序
  10.                 bubbleSort(chs);

  11.                 //数组变成字符串
  12.                 return new String(chs);//我自己写的,但是这点有疑惑,为啥不能返回String(shs),而是要new一下才能编译通过呢
  13.         }
  14.         
  15.         public static void bubbleSort(char[] arr)
  16.         {
  17.                 for(int x=0;x<arr.length-1;x++)
  18.                 {
  19.                         for(int y=0;y<arr.length-x-1;y++)
  20.                         {
  21.                                 if(arr[y]>arr[y+1])
  22.                                 
  23.                                 swap(arr,y,y+1);
  24.                         }
  25.                 }
  26.         }
  27.         public static void swap(char[] arr,int a,int b )
  28.         {
  29.                 char temp = arr[a];
  30.                 arr[a]= arr[b];
  31.                 arr[b]= temp;
  32.         }
  33.         
  34.         
  35.         
  36.         public static void sop(String str)
  37.         {
  38.                 System.out.println(str);
  39.         }
  40.         
  41.         public static void main(String[] args)
  42.         {
  43.                 String s ="abcedf";
  44.                 sop("("+reverseString(s)+")");
  45.         }
  46. }
复制代码

点评

啥问题?没明白  发表于 2013-4-16 19:47

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

5 个回复

倒序浏览
你程序的第46行错了,应该改成 StringTest.reverseString(s)
静态嘛,用类名打点调用就行了
回复 使用道具 举报
_王涛 发表于 2013-4-16 19:47
你程序的第46行错了,应该改成 StringTest.reverseString(s)
静态嘛,用类名打点调用就行了 ...

帅哥,我这是调用本类函数,又没有访问外部类,不需要你说的这个类名调用吧
回复 使用道具 举报
哦sorry
public class Test {
public static String reverseString(String s){
    char[] chs=s.toCharArray();
  bubbleSort(chs);
   return new String(chs);
}
public static void bubbleSort(char[] arr)
        {
                for(int x=0;x<arr.length-1;x++)
               {
                    for(int y=0;y<arr.length-x-1;y++)
                     {
                     if(arr[y]>arr[y+1])
                   swap(arr,y,y+1);
                       }
               }
        }
       public static void swap(char[] arr,int a,int b )
        {
              char temp = arr[a];
               arr[a]= arr[b];
               arr[b]= temp;
       }
public static void main(String[] args) {
   String s ="abcedf";
      System.out.print("("+reverseString(s)+")");
}
}
结果是----------》(abcdef),不对吗?
回复 使用道具 举报
_王涛 发表于 2013-4-16 20:59
哦sorry
public class Test {
public static String reverseString(String s){

结果是对的,我现在问的问题是为啥要返回的是  return new String(chs);  而不能return String(chs)
回复 使用道具 举报
本帖最后由 _王涛 于 2013-4-16 21:09 编辑

String也是一个类啊,你利用的是它的构造函数传参,构造函数创建对象new String(chs) ,其返回对应字符串。
如果直接String(chs)的话肯定不行了,如果是 return String.copyValueOf(chs);到还可以。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马