简体   繁体   English

错误:类型不兼容:U无法转换为int

[英]error: incompatible types: U cannot be converted to int

I have the following program. 我有以下程序。

import java.util.*;

public class Solution {


    public static <U extends Number> double median(List<U> l) {

        double Q = 0;

        int n = l.size();
        if (n%2 == 0) {
            Q = ((int)l.get(n/2) + (int)l.get(n/2-1)) / (double)2;
        } else {
            Q = new Double((int)l.get(n/2));
        }

        return Q;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        List<Integer> l = new ArrayList<>();


        for (int i = 0; i < n; i++){
            l.add(sc.nextInt());
        }

        Collections.sort(l);

        double Q1 = 0, Q2 = 0, Q3 = 0;

        List<Integer> lower = new ArrayList<>();
        List<Integer> upper = new ArrayList<>();

        if (n%2 == 0) {
            lower.addAll(l.subList(0, n/2));
            upper.addAll(l.subList(n/2, n));
        } else {
            lower.addAll(l.subList(0, n/2));
            upper.addAll(l.subList(n/2 + 1, n));
        }

        Q2 = median(l);
        Q1 = median(lower);
        Q3 = median(upper);

        System.out.format("%.0f%n", Q1);
        System.out.format("%.0f%n", Q2);
        System.out.format("%.0f%n", Q3);

        sc.close();

    }
}

It compiles fine and runs on my local machine, when I try to run it from eclipse, but when I submit it to hackerrank, I get the following compilation error. 当我尝试从eclipse运行它时,它可以很好地编译并在我的本地计算机上运行,​​但是当我将其提交给hackerrank时,出现以下编译错误。

Solution.java:12: error: incompatible types: U cannot be converted to int
            Q = ((int)l.get(n/2) + (int)l.get(n/2-1)) / (double)2;
                           ^
  where U is a type-variable:
    U extends Number declared in method <U>median(List<U>)
Solution.java:12: error: incompatible types: U cannot be converted to int
            Q = ((int)l.get(n/2) + (int)l.get(n/2-1)) / (double)2;
                                             ^
  where U is a type-variable:
    U extends Number declared in method <U>median(List<U>)
Solution.java:14: error: incompatible types: U cannot be converted to int
            Q = new Double((int)l.get(n/2));
                                     ^
  where U is a type-variable:
    U extends Number declared in method <U>median(List<U>)
3 errors

I don't even understand why I have to cast each list element back to an int, I thought since U extends Number, I should be able to add list elements without casting back to ints. 我什至不明白为什么我必须将每个列表元素都强制转换回int,我认为,由于U扩展了Number,所以我应该能够添加列表元素而不将其强制返回int。 Is there something I'm not understanding about generics? 关于泛型,我是否不了解? Q1, Q2, Q3 are guaranteed to be integers (but thats just because of the input test cases), but that shouldn't matter, why isn't my program compiling? 保证Q1,Q2,Q3是整数(但这仅是由于输入测试用例),但这没关系,为什么我的程序没有编译?

Why you declaring the median method as generic if you anyway casting the list items to integer? 如果将列表项强制转换为整数,为什么还要将中位数方法声明为泛型?

Change it to public static double median(List<Integer> l) and remove all the casting to int. 将其更改为public static double median(List<Integer> l)并删除所有强制转换为int的类型。

You can call the intValue() method on the Number elements instead of cast them to int. 您可以在Number元素上调用intValue()方法,而不是将其intValue()为int。

( https://docs.oracle.com/javase/8/docs/api/java/lang/Number.html#intValue-- ) https://docs.oracle.com/javase/8/docs/api/java/lang/Number.html#intValue--

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM