Trying to write an iterator that returns a Pair: Part of my 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;
}
}
getting
Cannot instantiate the type Pair
... although Pair is not an abstract class/interface. Why is this happening?
Make sure to import the correct class to initialize your pair object.
import org.apache.commons.math3.util.Pair;
Concrete Class -> Can directly create object.
This gives an error:
import org.apache.commons.lang3.tuple.Pair;
Its an abstract class, so you can't create the object directly.
Try instantiating org.apache.commons.lang3.tuple.ImmutablePair
(or org.apache.commons.lang3.tuple.MutablePair
) instead; those classes are concrete so they can be instantiated.
很可能是因为您没有向 Pair 添加泛型类型参数。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.