手记

使用Arrays.sort和冒泡排序分别对数字和字符串进行排序

采用两种方法对数组进行排序。
使用Arrays.sort和冒泡排序分别对数字和字符串进行排序


import java.util.Arrays;
public class Example5_3 {
    public static void main(String[] args) {
        int a[] = {21,43,32,-89,67};
        String st[] = {"door","apple","Applet","girl","boy"};
        Arrays.sort(a);
        Arrays.sort(st);
        System.out.println(Arrays.toString(a));
        System.out.println(Arrays.toString(st));
        int a1[] = {21,43,32,-89,67};
        String st1[] = {"door","apple","Applet","girl","boy"};
        for(int i = 0; i<a1.length-1;i++){
            for(int j = i+1;j<a1.length;j++){
                if(a1[j]<a1[i]){
                    a1[j] = a1[j]^a1[i];  //使用二进制数的异或运算规则进行交换数据。
                    a1[i] = a1[j]^a1[i];
                    a1[j] = a1[j]^a1[i];
                }
            }   
        }
        for(int i = 0; i<st1.length-1;i++){
            for(int j = i+1;j<st1.length;j++){
                if(st1[j].compareTo(st1[i])<0){
                    String temp;
                    temp = st1[j];
                    st1[j] = st1[i];
                    st1[i] = temp;
                }
            }   
        }
        System.out.println(Arrays.toString(a1));
        System.out.println(Arrays.toString(st1));
    }
}
0人推荐
随时随地看视频
慕课网APP