简体   繁体   English

为什么我不能输入:BlockingQueue<Integer> a = 新的 PriorityQueue&lt;&gt;(2);

[英]Why I can't type: BlockingQueue<Integer> a = new PriorityQueue<>(2);

The code I type in title doesn't compile.我在标题中输入的代码无法编译。 It says: Cannot infer arguments .它说: Cannot infer arguments

After that I did BlockingQueue<Integer> a = new PriorityBlockingQueue<>(2);之后我做了BlockingQueue<Integer> a = new PriorityBlockingQueue<>(2); which compiles just fine.编译得很好。 What was my mistake, so I can avoid it next time?我的错误是什么,所以我下次可以避免它?

Yes, I know that constructor in Queue interface has parameter(2) for initial capacity , while in BlockingQueue parameter(2) represents max elements .是的,我知道Queue接口中的构造函数具有用于初始容量的parameter(2) ,而在BlockingQueue parameter(2) 表示最大元素 Does this have anything to do with the error ?这与错误有什么关系吗?

Well for starters, PriorityQueue is not a BlockingQueue .对于初学者来说, PriorityQueue不是BlockingQueue It wont be able to infer any generic arguments for PriorityQueue<T> because there is no T that will be valid.它无法推断PriorityQueue<T>任何通用参数,因为没有有效的T That being said, Cannot infer arguments sounds like it may be due to a side effect of this change somewhere else in your code.话虽如此, Cannot infer arguments听起来可能是由于代码中其他地方的这种更改的副作用。

Looking at the documentation for BlockingQueue , you should instead use one of the classes under "All Known Implementing Classes:", find a library with BlockingQueue s, or implement your own.查看BlockingQueue文档,您应该改用“所有已知实现类:”下的类之一,找到一个带有BlockingQueue的库,或者实现您自己的。

Your other option is to use another type of queue instead, like AbstractQueue<Integer> in place of BlockingQueue<Integer> .您的另一个选择是使用另一种类型的队列,例如AbstractQueue<Integer>代替BlockingQueue<Integer>

The code in the title doesn't compile because PriorityQueue does not implement nor extend BlockingQueue .标题中的代码无法编译,因为PriorityQueue没有实现也没有扩展BlockingQueue It's like doing this: LinkedList<Integer> ls = new ArrayList<>(): , which will not compile.就像这样做: LinkedList<Integer> ls = new ArrayList<>(): ,它不会编译。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我不能添加 PriorityQueue? - Why can't I add on PriorityQueue? 为什么我不能用BlockingQueue实例化ThreadPoolExecutor <Callable> ; 为什么只有BlockingQueue <Runnable> ? - Why can't i instantiate ThreadPoolExecutor with BlockingQueue<Callable>; why only BlockingQueue<Runnable>? 为什么我不能从 PriorityQueue 中删除 peek() 获取的元素? - Why can't I remove an element gotten by peek() from the PriorityQueue? 如何将元素添加到不同类型的 Java 集合的 PriorityQueue 中? - How can i add elements to a PriorityQueue of Java Collections that are of different type? 为什么我可以通过Iterator看到PriorityQueue元素,而不能通过poll看到? - Why can I see PriorityQueue elements with an Iterator, but not with poll? 为什么不能将此字符串转换为整数? - Why can't I convert this string into an integer? 为什么 Spring ThreadPoolTaskExecutor#createQueue 指定 BlockingQueue<runnable> 作为接受提交 Callable 时的返回类型<t></t></runnable> - Why does Spring ThreadPoolTaskExecutor#createQueue specify BlockingQueue<Runnable> as return type when it accepts submitting Callable<T> BlockingQueue 无法从 JTextField 读取字符串 - BlockingQueue can't read String from JTextField 为什么 PriorityQueue 需要 Comparable? - Why do I need Comparable for PriorityQueue? 为什么我不能在泛型方法中将+运算符与Integer和Double一起使用? - Why I can't use + operator with Integer and Double in generic method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM