Java声明通用对象

时间:2016-08-03 00:33:05

标签: java generics

import java.util.ArrayList;

public class PermutationList<T> extends ArrayList<T> implements Comparable<PermutationList<T>>{
    @Override
    public int compareTo(PermutationList<T> o) {
        if(this.size() < o.size()) return -1;
        if(this.size() > o.size()) return 1;
        int size = size();
        for(int i = 0; i < size; i++) {
            int cmp = get(i).compareTo(o.get(i));
            if(cmp != 0) return cmp;
        }
        return 0;
    }   
}

附件是我正在做的一个简单的课程。 compareTo()函数调用产生错误,因为尚未声明通用对象T实现Comparable。如何声明T以实现Comparable?

1 个答案:

答案 0 :(得分:0)

谢谢Thilo,你回答了我的问题。这是我的示例源代码。希望它对其他人有用。

package padpartition;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.TreeSet;

public class Selection<T extends Comparable<T>> {
    public static <T> void selection(List<List<T>> input, List<List<T>> output){
       int length = input.get(0).size();
       int square = length * length;
       for(int k = 0; k < input.size(); k++) {
           for(int i = 1; i < square; i++) {
               List<T> elementlist = new ArrayList<T>(input.get(k));
               int num = 0;
               for(int j = 0; j < length; j++) {
                   num = (1 << j) & i;
                   if(num == 0) {
                       elementlist.set(j, null);
                   }
               }
               output.add(elementlist);
           }
       }
    }

    public static <T> void trim(List<List<T>> list) {
        for(List<T> element : list) {
            for(int i = 0; i < element.size(); i++) {
                if(element.get(i) == null) {
                    element.remove(i);
                    i--;
                }
            }
        }
    }

    private static <T extends Comparable<T>>void sort(List<List<T>> list) {
        TreeSet<List<T>> tset = new TreeSet<List<T>>(new PermutationComparator());
        for(List<T> element : list) {
            tset.add(element);
        }
        list.clear();
        list.addAll(tset);
    }

    public static void main(String[] args) {
        List<List<Integer>> output = new ArrayList<List<Integer>>();
        Permute.permute(java.util.Arrays.asList(1,2,3), output, 0);
        List<List<Integer>> output2 = new ArrayList<List<Integer>>();
        Selection.selection(output, output2);
        trim(output2);
        sort(output2);
        Permute.printList(output2);        
    }
}


package padpartition;

import java.util.ArrayList;
import java.util.List;

public class Permute<T> {

    public static <T> void permute(List<T> arr, List<List<T>> output, int k){
        for(int i = k; i < arr.size(); i++){
            java.util.Collections.swap(arr, i, k);
            permute(arr, output, k+1);
            java.util.Collections.swap(arr, k, i);
        }
        if (k == arr.size() -1){
            output.add(new ArrayList<T>(arr));
        }
    }

    public static <T> void printList(List<List<T>> output) {
        for(List<T> num : output) {
            for(int i = 0; i < num.size(); i++) {
                if(i < num.size()-1) {
                    System.out.print(num.get(i) + ", ");
                }
                else System.out.println(num.get(i));
            }
        }
    }

    public static void main(String[] args){
        List<List<Integer>> output = new ArrayList<List<Integer>>();
        Permute.permute(java.util.Arrays.asList(3,4,6,1), output, 0);
        printList(output);
    }
}


package padpartition;

import java.util.Comparator;
import java.util.List;

public class PermutationComparator<T extends Comparable<T>> implements Comparator<List<T>> {
    @Override
    public int compare(List<T> one, List<T> two) {
        if(one.size() < two.size()) return -1;
        if(one.size() > two.size()) return 1;
        int size = one.size();
        for(int i = 0; i < size; i++) {
            int cmp = one.get(i).compareTo(two.get(i));
            if(cmp != 0) return cmp;
        }
        return 0;
    }
}