我现在正在开发一个Java项目,我有一个名为DistanceQueue的类.它的签名是由
public class DistanceQueue<Integer> extends PriorityQueue<Integer>
Run Code Online (Sandbox Code Playgroud)
在这个课程中,有一种方法
public boolean add(int v)
Run Code Online (Sandbox Code Playgroud)
它将键值对(v,Double.MAX_VALUE)添加到名为DistanceQueue类的称为距离的HashMap中.但是,在add(int v)里面,当我输入时
distances.put(v, Double.MAX_VALUE);
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
DistanceQueue.java:98: error: no suitable method found for put(int,double)
distances.put(v, Double.MAX_VALUE);
^
method HashMap.put(Integer,Double) is not applicable
(actual argument int cannot be converted to Integer by method invocation conversion)
where Integer is a type-variable:
Integer extends Object declared in class ShortestPaths.DistanceQueue
1 error
Run Code Online (Sandbox Code Playgroud)
有谁知道我为什么会收到这个错误?我认为Java会自动在int和Integer之间进行转换.有一种简单的方法可以解决它吗?
谢谢!
aio*_*obe 19
您正在使用Integer作为类型参数的名称,它隐藏了java.lang.Integer.
public class DistanceQueue<Integer> extends PriorityQueue<Integer>
^^^^^^^
Run Code Online (Sandbox Code Playgroud)
您应该只删除type参数:
public class DistanceQueue extends PriorityQueue<Integer>
Run Code Online (Sandbox Code Playgroud)