【问题标题】:"The inferred type _____ is not a valid substitute for the bounded parameter"“推断的类型 _____ 不是有界参数的有效替代品”
【发布时间】:2014-10-22 02:16:58
【问题描述】:
public class Main {    
    public static void main(String[] args) throws FileNotFoundException {
        ArrayList<Country> cList = new ArrayList<Country>();
        ArrayList choice = new ArrayList();
        File inf = new File("src/countrydata.txt");
        Scanner scan = new Scanner(inf).useDelimiter("[\t|\n|\r]+");
        Scanner s = new Scanner(System.in);
        int p = 0;
        double g = 0;
        while(scan.hasNext()){
            cList.add(new Country(scan.next(), scan.nextInt(), scan.nextDouble()));
        }

        System.out.print("Would you like in sorted my name(n), population(p) or growth(g): ");
        String go = s.next();
        go = go.toLowerCase();
        if(go.equals("n")){
            choice.add(go);
        }else if(go.equals("p")){
            choice.add(p);
        }else if(go.equals("g")){
            choice.add(g);
        }

        MyUtil.bubbleSort(cList, choice);

我的错误:(上一行)绑定不匹配:MyUtil 类型的通用方法 bubbleSort(List, List) 不适用于参数 (ArrayList, ArrayList)。推断类型 Country 不是有界参数的有效替代 > }

//Sort Class

public class MyUtil <E extends Comparable<E>>{

    public static <E extends Comparable<E>>void bubbleSort(List<E> list, List choice){
        int n = list.size();
        boolean swap = true;
        while(swap){
            swap = false;
            for(int i = 0; i < n-1; i++){
                if(list.get(i).compareTo(list.get(i+1)) == 1){
                    swap(list, i, i+1);
                    swap = true;
                }
            }
        }
    }

    public static <E extends Comparable<E>>void swap(List<E> list, int i, int j){
        E temp = list.get(i);
        list.set(i, list.get(j));
        list.set(j, temp);
    }
}


public class Country {
    private String name;
    private int population;
    private double growth;

    public Country(String name, int population, double growth){
        this.name = name;
        this.population = population;
        this.growth = growth;
    }

    public String getName(){return name;}
    public int getPopulation(){return population;}
    public double getGrowth(){return growth;}   

    public String toString(){
        return name + ", population of " + population + ", with an anual growth of: " + growth + "."; 
    }

    public int compareTo(Country c, String s){
        if(name.substring(0, 1).compareTo(c.getName().substring(0, 1)) > 0){
            return -1;
        }else if(name.substring(0, 1).compareTo(c.getName().substring(0, 1)) == 0){
            return 0;
        }else{
            return 1;
        }
    }

    public int compareTo(Country c, int p){
        if(population < c.getPopulation()){
            return -1;
        }else if(population == c.getPopulation()){
            return 0;
        }else{
            return 1;
        }
    }

    public int compareTo(Country c, double g){
        if(growth < c.getGrowth()){
            return -1;
        }else if(growth == c.getGrowth()){
            return 0;
        }else{
            return 1;
        }
    }
}

【问题讨论】:

  • 你能为你的问题选择更好的标题吗?
  • 我可以,但我是新手,我真的不知道该怎么称呼它:/
  • 你甚至没有使用你的第二个论点;为什么要麻烦把它放在那里?

标签: java arraylist


【解决方案1】:

问题是您在该行中指定了

public static <E extends Comparable<E>>void bubbleSort(List<E> list, List choice)

E 必须扩展 Comparable&lt;E&gt;Country 没有。为了让它编译,你必须改变

public class Country {

public class Country implements Comparable<Country> { 

你还必须实施

public int compareTo(Country c) {}

但这样做不会让您灵活地按多个不同维度进行排序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多