当前位置: 首页 > 知识库问答 >
问题:

“推断的类型_____不是有界参数的有效替代品”

微生翼
2023-03-14
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;
        }
    }
}

共有1个答案

阎咏思
2023-03-14

问题是您在行中指定了

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

e必须扩展carable ,而country则不扩展。为了使其编译,您必须更改

public class Country {

public class Country implements Comparable<Country> { 
public int compareTo(Country c) {}
 类似资料:
  • 问题内容: 考虑以下代码: 排序调用给出错误: 绑定不匹配:类型为Collections的通用方法sort(List )不适用于参数(ArrayList >)。推断的类型MyItem 不是有效替代边界参数<T扩展Comparable <?超级T>> 为什么会这样呢? 如果实施该工具,那为什么不能替代呢? 抱歉,是否有人提出这个问题,但我觉得这个问题有些具体。 问题答案: 实际上,对该错误的更详细说

  • 我正面临以下问题: “链”来自ViewModelHelper类定义 2) 2.1),->可以用替换 如果我将2.1)应用于1.1)&1.2),我们可以看到,参数T是一致的 从1)遵循从2)遵循,从2.1)遵循可以被替换,如果我正确理解,这个错误应该不会出现,有人能解释一下吗?为什么eclipse会给我这个错误? 谢谢!

  • 在以下代码中: 由于lambda不可序列化,以下代码行无法编译: 我的问题是,让lambda实现可序列化有什么语法上的好处吗,还是我必须让它成为一个非匿名类?

  • 我得到一个错误:::绑定不匹配:类型MyClass1不是Person类型的有界参数的有效替代品

  • OpenGL定义了C函数来管理资源。我编写了一个简单的包装器来以RAII的方式处理它们。函数对类似于和。但是,也有一些函数对适用于资源数组,例如和。对于前者,我编写了一个简单的类来完成这项工作,对于后者,我编写了另一个处理数组的类。然而,我注意到有时我只使用一个缓冲区或纹理,在那里我不必承担向量的费用,我想如果发布函数在开始时采用大小参数,我会专门化类析构函数,但是... 对于上述SSCCE g树