compareTo

compareTo :

摘录:https://blog.csdn.net/u010356768/article/details/71036301

1、返回参与比较的前后两个字符串的ASCII码的差值

2、两个字符串首字母不同,则该方法返回首字母的ASCII码的差值

3、参与比较的两个字符如果首字符相同,则比较下一个字符,直到有不同的为止,返回该不同支付的ASCII码差值

4、两个字符串不一样长,可以参与比较的字符又完全一样,则返回两个字符串的长度差值。

5、项目中的用途:比较版本号的高低。

// 排序

public class test {
    public static void main(String[] args) {
        Integer[] integers = {new Integer(7),new Integer(2),new Integer(3),new Integer(4)}; //7,2,3,4
        sort(integers);
        printList(integers);
    }
    
    public static <E extends Comparable<E>> void sort(E[] list) {
        E currentMin;
        int currentMinIndex;
        for (int i=0; i<list.length -1; i++) {
            currentMin = list[i];
            currentMinIndex = i;
            for(int j=i+1; j<list.length; j++) {
                if (currentMin.compareTo(list[j]) > 0) {        //排出最小值
                    currentMin = list[j];                
                    currentMinIndex = j;
                }
            }
            if (currentMinIndex != i) {                            //把最小值放置那个位置
                list[currentMinIndex] = list[i];
                list[i] = currentMin;
            }
        }
    }
    
    public static void printList(Object[] list) {
        for(int i=0; i<list.length; i++) {
            System.out.print(list[i] + " ");    //2 3 4 7 
        }
        System.out.println();
    }
}

 

拼命敲
原文地址:https://www.cnblogs.com/wuyuwuyueping/p/9584248.html