简体   繁体   中英

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.

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. 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?

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.

You can call the intValue() method on the Number elements instead of cast them to int.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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