【问题标题】:How can I shorten this Java code? [closed]如何缩短此 Java 代码? [关闭]
【发布时间】:2017-08-18 15:48:31
【问题描述】:
/* 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()); 
    }

}

我只是想知道我这样做是对的还是有更好的更短版本?

【问题讨论】:

标签: java


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-05
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 2020-05-26
      相关资源
      最近更新 更多