对象Java arraylist

时间:2016-06-10 13:20:12

标签: java arraylist generic-programming

我有一个带有对象字段的数组列表4.我需要知道如何订购我的对象的字段"逐个"使用执行算法的通用方法(isort,msort,qsort)。

我知道比较器接口,但不知道如何检查泛型类型T中的所有字段。

1 个答案:

答案 0 :(得分:0)

如果我理解你是对的,那么你就有了一个作为泛型类的类,并希望知道如何使用Comparator接口。这是我的尝试:

class ClassWithFields {

    int fieldA;
    double fieldB;
    String fieldC;

    public ClassWithFields(int fieldA, double fieldB, String fieldC) {
        this.fieldA = fieldA;
        this.fieldB = fieldB;
        this.fieldC = fieldC;
    }

    @Override
    public String toString() {
        return "["+fieldA+","+fieldB+","+fieldC+"]";
    }

}

public class GenericComparator<T extends ClassWithFields> implements Comparator<T> {

    @Override
    public int compare(T o1, T o2) {
        if (o1.fieldA < o2.fieldA)
            return -1;
        else if (o1.fieldA > o2.fieldA)
            return +1;
        else if (o1.fieldB < o2.fieldB)
            return -1;
        else if (o1.fieldB > o2.fieldB)
            return +1;
        else
            return o1.fieldC.compareTo(o2.fieldC);
    }

    public static void main(String[] args) {
        ClassWithFields[] cwfArray = new ClassWithFields[3];
        cwfArray[0] = new ClassWithFields(2, 1.5, "Test");
        cwfArray[1] = new ClassWithFields(1, 3.5, "Test");
        cwfArray[2] = new ClassWithFields(2, 1.5, "Tast");
        Arrays.sort(cwfArray, new GenericComparator<ClassWithFields>());
        System.out.println(Arrays.toString(cwfArray));
    }

}

运行main方法时,第一个数组项将放在数组的后面,因为1&lt; 2和&#39; a&#39; &LT; &#39; E&#39; (也是3.5&lt; 1.5但我们不去那里,因为我们有一个比较字段的订单) 这回答了这个问题吗?

编辑:现在应该这样做。

public class GenericComparator<T extends ClassWithFields> implements Comparator<T> {

    private String fieldIdentifier;

    public GenericComparator(String fieldIdentifier)
    {
        this.fieldIdentifier = fieldIdentifier;
    }

    @Override
    public int compare(T o1, T o2) {
        if (fieldIdentifier.equals("fieldA")) {
            if (o1.fieldA < o2.fieldA)
                return -1;
            else if (o1.fieldA > o2.fieldA)
                return +1;
            return 0;
        }
        else if (fieldIdentifier.equals("fieldB")) {
            if (o1.fieldB < o2.fieldB)
                return -1;
            else if (o1.fieldB > o2.fieldB)
                return +1;
            return 0;
        }
        else
            return o1.fieldC.compareTo(o2.fieldC);
    }

    public static <S extends ClassWithFields> void isort(ArrayList<S> array, String type) {
        Comparator<S> comp = new GenericComparator<S>(type);
        // TODO employ search algorithm
        S help = null;
        for (int i = 0; i < array.size(); i++) {
            for (int j = 0; j < array.size(); j++) {
                if (comp.compare(array.get(i), array.get(j)) > 0) {
                    help = array.get(i);
                    array.set(i, array.get(j));
                    array.set(j, help);
                }
            }
        }
    }

    public static void main(String[] args) {
        ClassWithFields[] cwfArray = new ClassWithFields[3];
        cwfArray[0] = new ClassWithFields(2, 1.5, "Test");
        cwfArray[1] = new ClassWithFields(1, 3.5, "Test");
        cwfArray[2] = new ClassWithFields(2, 1.5, "Tast");
        ArrayList<ClassWithFields> cwfList = new ArrayList<ClassWithFields>();
        Collections.addAll(cwfList, cwfArray);
        isort(cwfList, "fieldA");
        System.out.println(Arrays.toString(cwfList.toArray()));
    }

}