如何缩短此Java代码?

时间:2017-08-18 15:48:32

标签: java

/* This program sorts out name in orders from
their first alphabetical orders .*/

package nameorder;

public class NameOrder {

    public static void sayName(String a, String s, String d){
        System.out.println("Name By Alphabetical Order: \n1."+a+"\n"+"2."+s+"\n3."+d+"\n");

    }

    public static void stringOrder(String a ,String s ,String d){
        int i= a.compareTo(s) ;
        int j= a.compareTo(d) ;
        int k= d.compareTo(s) ;
        int l= d.compareTo(a) ;
        String first="";
        String second="";
        String third="";
            if(i<0&&j<0){
                first=a;
                    if(k>0&&l>0){
                        third = d;
                        second = s;
                    }else{
                        second = d;
                        third = s;
                    }
            }else if(i>0&&j>0){
                third=a;
                    if(k<0&&l<0){
                        first = d;
                        second = s;
                    }else{
                        second = s;
                        first = d;
                    }
            }else{
                second=a;
                    if(k<0&&l<0){
                        first = d;
                        third = s;
                    }else{
                        first = s;
                        third = d;
                    }
            }
        sayName(first,second,third);    
    }

    public static void main(String[] args) {
        String a ="C";
        String s ="a";
        String d ="h";
        stringOrder(a.toUpperCase(),s.toUpperCase(),d.toUpperCase()); 
    }

}

我只是想知道我是否做得对,或者有更好的短版本?

2 个答案:

答案 0 :(得分:0)

我在这里使用了Collections.List,以便您的代码更具动态性,允许任何代码 要订购的字符串数量。有了你的,你硬编码将输入3个字符串。在这里,您可以在main函数中输入任意数量的字符串。

Collections.Sort方法将以最有效的方式对列表进行排序。无论何时你都可以使用Java上的人员制作的方法,因为他们花了数年时间来优化这些功能。

public static void main(String[] args){
    // Create a collection to store the strings in
    List<String> list = new ArrayList<String>();

    // Add the strings to the Collection
    list.add("C");
    list.add("A");
    list.add("H");

    // Use the Collections library to sort the strings for you, by their
    // defined ordering
    Collections.sort(list);

    // Print the ordered strings
    System.out.println("Names By Alphabetical Order: ");
    for(String string: list){
        System.out.println(string);
    }
}

答案 1 :(得分:0)

从“排序三个字符串”的角度来看,您可能只需要进行三次比较并丢失所有这些临时变量。

public static void stringOrder(String a, String s, String d) {
    String tmp;

    if (a.compareTo(s) > 0) {
        tmp = a;
        a = s;
        s = tmp;
    }

    if (a.compareTo(d) > 0) {
        tmp = a;
        a = d;
        d = tmp;
    }

    if (s.compareTo(d) > 0) {
        tmp = s;
        s = d;
        d = tmp;
    }

    sayName(a, s, d);
}

但从可维护性的角度来看,只需使用Java内置的工具一次对多个字符串进行排序:

public static void stringOrder(String a, String s, String d) {
    String [] arr = {a, s, d};
    java.util.ArrayList<String> list = new ArrayList<String>(Arrays.asList(arr));
    java.util.Collections.sort(list);
    sayName(list.get(0), list.get(1), list.get(2));
}