error: cannot infer type arguments for PriorityQueue<> PriorityQueue<Integer> pq = new PriorityQueue<>( ^
时间: 2024-02-10 11:26:49 浏览: 192
这个错误是由于无法推断出优先队列 `PriorityQueue` 的类型参数导致的。为了解决这个问题,您可以明确指定类型参数的类型。在您的代码中,您使用了 `Integer` 类型,所以您可以修改代码如下:
```java
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(
intervals.length, (Integer a, Integer b) -> a - b);
```
通过在 `PriorityQueue` 的构造函数中指定类型参数为 `Integer`,编译器就可以正确推断出类型参数,并解决错误。
希望能解决您的问题!如果您还有其他问题,请随时提问。
相关问题
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.
Cannot infer type arguments for HashMap<>是为什么
这个错误通常发生在你没有指定 HashMap 的键和值类型时。HashMap 是一个泛型类,需要在创建实例时指定键和值的类型。例如,如果你想要创建一个键为 String 类型,值为 Integer 类型的 HashMap,应该这样写:
```
HashMap<String, Integer> map = new HashMap<>();
```
如果你没有指定键和值的类型,例如下面的代码:
```
HashMap map = new HashMap();
```
那么编译器就无法推断出 HashMap 的类型参数,从而导致"Cannot infer type arguments for HashMap<>"的错误。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)