zoukankan      html  css  js  c++  java
  • Java经典编程题50道之四十

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

    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;
        }
    }

  • 相关阅读:
    C语言学习第八章
    C语言学习第七章
    C语言学习第六章
    C语言学习第五章
    ssh的bug
    Oracel 用户管理
    初识Kettle
    IDEA使用MAVEN时自动创建骨架卡的设置
    2017/6/12 JSON
    DDL,DML,DQL
  • 原文地址:https://www.cnblogs.com/qubo520/p/6962582.html
Copyright © 2011-2022 走看看