hop*_*ull 0 java generics instantiation
尝试编写一个返回 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;
}
}
Run Code Online (Sandbox Code Playgroud)
得到
无法实例化类型 Pair
...虽然 Pair 不是抽象类/接口。为什么会发生这种情况?
小智 5
确保导入正确的类来初始化您的配对对象。
import org.apache.commons.math3.util.Pair;
Run Code Online (Sandbox Code Playgroud)
具体类 -> 可以直接创建对象。
这给出了一个错误:
import org.apache.commons.lang3.tuple.Pair;
Run Code Online (Sandbox Code Playgroud)
它是一个抽象类,因此您不能直接创建该对象。
| 归档时间: |
|
| 查看次数: |
8391 次 |
| 最近记录: |