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

  • 相关阅读:
    Map 循环出key 和 value
    Jquery Validate
    Cookie/Session机制详解
    Java根据sessionId获取Session对象
    在线用户统计二
    页面在线访问人数统计&&在线登录人数统计一
    在线会话管理
    oneworld元数据配置
    java map遍历
    黑马程序员——C语言位运算符
  • 原文地址:https://www.cnblogs.com/qubo520/p/6962582.html
Copyright © 2011-2022 走看看