Java Generics(工作代码,但不漂亮..也没有效率)

时间:2017-04-26 22:26:56

标签: java generics arraylist set

因此,这种令人憎恶的代码会根据用户的输入创建数组。样本输入将类似于" S 4 aba bab baa aab"其中:

args [0] =数组类型(Int,Double,String)

args [1] =数组的长度

args [2 ...] =数组的内容

在我的代码中创建数组是可怕的,但我不太确定在使用泛型时如何做到这一点。代码的要点是能够将任何创建的数组传递给底部的方法,因此是泛型。

感谢您提前输入任何内容!

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;

public class ArrayGenerics<E> {
    public static void main(String[] args) throws FileNotFoundException {

    checkUsage(args);

我的问题在这里: 我有循环来填充数组并调用每个条件中的方法,这让我觉得我可以用这样的方式重写它,它不是那么重复。

    if (args[0].matches("I|i")) {
        ArrayList<Integer> iL = new ArrayList<Integer>();
        for (int i = 2; i < args.length; i++) {
            iL.add(Integer.parseInt(args[i]));
        }
        System.out.println("Original: " + iL);
        System.out.println("  Unique: " + removeDuplicates(iL));
        shuffle(iL);
        System.out.println("Shuffled: " + iL);
        System.out.println(" Maximum: " + max(iL));

    } else if (args[0].matches("S|s")) {
        ArrayList<String> sL = new ArrayList<String>();
        for (int i = 2; i < args.length; i++) {
            sL.add(args[i]);
        }
        System.out.println("Original: " + sL);
        System.out.println("  Unique: " + removeDuplicates(sL));
        shuffle(sL);
        System.out.println("Shuffled: " + sL);
        System.out.println(" Maximum: " + max(sL));
    } else {
        ArrayList<Double> dL = new ArrayList<Double>();
        for (int i = 2; i < args.length; i++) {
            dL.add(Double.parseDouble(args[i]));
        }
        System.out.println("Original: " + dL);
        System.out.println("  Unique: " + removeDuplicates(dL));
        shuffle(dL);
        System.out.println("Shuffled: " + dL);
        System.out.println(" Maximum: " + max(dL));
    }

}

有问题的代码结尾

public static <E> ArrayList<E> removeDuplicates(ArrayList<E> list) {
    new HashSet<E>(list);
    list = new ArrayList<E>(new HashSet<E>(list));              
    return list;

}

public static <E> void shuffle(ArrayList<E> list) {
    Collections.shuffle(list);
}

public static <E extends Comparable<E>> E max(ArrayList<E> list) {
    return Collections.max(list);
}

public static void checkUsage(String[] args)
{
    if (args.length < 2) 
    {
        System.out.println("Please supply more than 2 arguments");
        System.exit(1);
    }
    if (!(args[0]=="s") && (args[0]=="S") &&
        (args[0]=="d") && (args[0]=="D") &&
        (args[0]=="i") && (args[0]=="I")){
        System.out.println("Incorrect format of command.");
        System.exit(1);
    }
}
}

1 个答案:

答案 0 :(得分:0)

不是最好的做法,但是如果你正在尝试编码高尔夫,你可以缩小它:

public static <E extends Comparable<E>> void main(String[] args) throws FileNotFoundException {

    checkUsage(args);

    ArrayList<E> array = new ArrayList<>();

    if (args[0].matches("I|i")) 
        for (int i = 2; i < args.length; i++) 
            array.add((E) new Integer(args[i]));

     else if (args[0].matches("S|s")) 
         for (int i = 2; i < args.length; i++) 
                array.add((E) new String(args[i]));
     else 
         for (int i = 2; i < args.length; i++) 
                array.add((E) new Double(args[i]));


    System.out.println("Original: " + array);
    System.out.println("  Unique: " + removeDuplicates(array));
    shuffle(array);
    System.out.println("Shuffled: " + array);
    System.out.println(" Maximum: " + max(array));

}

效率更低......

public static <E extends Comparable<E>> void main(String[] args) throws FileNotFoundException {

    checkUsage(args);

    ArrayList<E> array = new ArrayList<>();

    for (int i = 2; i < args.length; i++)
        if (args[0].matches("I|i"))
            array.add((E) new Integer(args[i]));
        else if (args[0].matches("S|s"))
            array.add((E) new String(args[i]));
        else if (args[0].matches("D|d"))
            array.add((E) new Double(args[i]));

    System.out.println("Original: " + array);
    System.out.println("  Unique: " + removeDuplicates(array));
    shuffle(array);
    System.out.println("Shuffled: " + array);
    System.out.println(" Maximum: " + max(array));

}