下面的代码片段不能编译;我有一个泛型类,映射从泛型键K到浮点。我的大多数方法都是以一种真正通用的方式处理这张地图。通用类中的特定类型的约束方法
此外,我想允许用升序整数值初始化映射。显然,这要求我只将方法限制为K = Integer。不幸的是,我得到以下错误:
The method put(K, Float) in the type Map is not applicable for the arguments (K extends Integer, Float)
任何帮助在这里是高度赞赏!
public class MyClass<K> {
Map<K,Float> myMap;
<K extends Integer> MyClass(int size){
distribution = new HashMap<K,Float>();
Integer bar = 0;
while(bar<size){
Float rnd = ThreadLocalRandom.current().nextFloat();
myMap.put(bar,rnd);
bar++;
}
}
}
有没有'return'在'SparsePDT'方法o.o – ItamarG3
你知道,'Integer'声明'final'? –
@ItamarGreen谢谢,这只是一个人造物,然后再将其降低为一个玩具的例子。该方法实际上是一个构造函数。 – Chris