首页 > 编程语言 > 详细

Java经典编程题50道之四十

时间:2017-06-08 14:48:19      阅读:268      评论:0      收藏:0      [点我收藏+]

将几个字符串排序(按英文字母的顺序)。

public class Example40 {
    public static void main(String[] args) {
        String[] s={"math","english","java","java web","rose"};
        stringSort(s);
    }

    public static void stringSort(String[] s) {
        String temp = null;
        
        for (int i = 0; i < s.length; i++) {
            for (int j = i + 1; j < s.length; j++) {
                if (compare(s[i], s[j])) {
                    temp = s[i];
                    s[i] = s[j];
                    s[j] = temp;
                }
            }
        }
        for (int i = 0; i < s.length; i++) {
            System.out.println(s[i]);
        }
    }
    public static boolean compare(String s1, String s2) {
        boolean result = true;
        for (int i = 0; i < s1.length() && i < s2.length(); i++) {
            if (s1.charAt(i) > s2.charAt(i)) {
                result = false;
                break;
            } else if (s1.charAt(i) < s2.charAt(i)) {
                result = true;
                break;
            } else {
                if (s1.length() < s2.length()) {
                    result = true;
                } else {
                    result = false;
                }
            }
        }
        return result;
    }
}

Java经典编程题50道之四十

原文:http://www.cnblogs.com/qubo520/p/6962582.html

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