【发布时间】:2017-08-20 16:02:01
【问题描述】:
尝试编写一个返回 Pair 的迭代器: 我的 PairIterator 的一部分
public Pair next() {
this.counter ++;
Pair p = new Pair(this.l.get(counter - 1), this.l.get(counter));
//error occurs here
}
public class Pair<E> {
private E e1;
private E e2;
public Pair(E e1, E e2) {
this.e1 = e1;
this.e2 = e2;
}
public E first() {
return this.e1;
}
public E second() {
return this.e2;
}
}
得到
无法实例化类型 Pair
...虽然 Pair 不是抽象类/接口。为什么会这样?
【问题讨论】:
-
this.counter 是什么?没有提到任何地方!
-
我在这里没有看到“新配对”的例子,所以我们无法猜测你做了什么。
-
counter 可能是 long 或 ind,但是 this.l 是什么类型?
-
欢迎来到 Stack Overflow!请拿起tour,环顾四周,并通读help center,尤其是How do I ask a good question? 和What topics can I ask about here?。请提供Minimal, Complete, and Verifiable example (MCVE)
-
@bmargulies "new Pair" 出现在 sn-p 的第 3 行。
标签: java generics instantiation