error: cannot infer type arguments for PriorityQueue<> PriorityQueue<Integer> pq = new PriorityQueue<>( ^
时间: 2024-02-10 08:26:49 浏览: 181
这个错误是由于无法推断出优先队列 `PriorityQueue` 的类型参数导致的。为了解决这个问题,您可以明确指定类型参数的类型。在您的代码中,您使用了 `Integer` 类型,所以您可以修改代码如下:
```java
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(
intervals.length, (Integer a, Integer b) -> a - b);
```
通过在 `PriorityQueue` 的构造函数中指定类型参数为 `Integer`,编译器就可以正确推断出类型参数,并解决错误。
希望能解决您的问题!如果您还有其他问题,请随时提问。
相关问题
private static final Map<String, ISMSSendAPI> handlerMap = new HashMap<>() 报错 Cannot infer type arguments for HashMap<>
这个错误是因为Java编译器无法推断HashMap的类型参数。可以尝试在HashMap后面指定类型参数,如下所示:
```
private static final Map<String, ISMSSendAPI> handlerMap = new HashMap<String, ISMSSendAPI>();
```
或者,你可以使用钻石操作符来让编译器推断类型参数:
```
private static final Map<String, ISMSSendAPI> handlerMap = new HashMap<>();
```
这样编译器就会根据变量声明的类型来自动推断HashMap的类型参数。
Cannot infer type arguments for HashMap<>
`HashMap<>` is a generic class in Java, which means that it requires type arguments to be specified. You need to specify the type of keys and values. For example, if you want a `HashMap` that maps strings to integers, you would declare it as follows:
```java
HashMap<String, Integer> map = new HashMap<String, Integer>();
```
In this case, `String` is the type of keys, and `Integer` is the type of values. If you want to use type inference, you can omit the type arguments on the right-hand side of the assignment, and Java will infer them from the context:
```java
HashMap<String, Integer> map = new HashMap<>();
```
Here, Java infers the type arguments based on the left-hand side of the assignment.
阅读全文