LAPTOP-8KA88UT5_20200905-陈浩

问题

1、输入若干个单词放入数组中,(忽略大小写)排序后输出

public class Words {
    public String[] compare(String[] s) {
        for (int i = 0; i < s.length - 1; i++) {
            for (int j = 0; j < s.length - 1 - i; j++) {
                if (s[j].compareToIgnoreCase(s[j + 1]) > 0) {
                    String temp = s[j];
                    s[j]=s[j+1];
                    s[j+1]=temp;
                }
            }
        }
        return s;
    }
    public static void main(String[] args) {
        Words w = new Words();
        String [] words = {"hello","AS" ,"as","Bei"};
        String [] ss = w.compare(words);
        System.out.println(Arrays.toString(ss));
    }
} 

输出结果为:

[AS, as, Bei, hello]

吐槽

标签

© 2021 成都云创动力科技有限公司 蜀ICP备20006351号-1