相关疑难解决方法(0)

使用两个不同的泛型参数调用泛型函数仍然可以编译

以下代码甚至可以编译怎么可能?据我所知,count函数是用两种不同的类型调用的,但编译器没有抱怨并且很乐意编译这段代码.

public class Test {
        public static <T> int count(T[] x,T y){
                int count = 0;
                for(int i=0; i < x.length; i++){
                        if(x[i] == y) count ++;
                }
                return count;  
        }
        public static void main(String[] args) {
                Integer [] data = {1,2,3,1,4};
                String value = "1";
                int r =count(data,value);
                System.out.println( r + " - " + value);
        }
}
Run Code Online (Sandbox Code Playgroud)

java generics compiler-errors compilation

4
推荐指数
1
解决办法
196
查看次数

标签 统计

compilation ×1

compiler-errors ×1

generics ×1

java ×1