[英]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.