Consider this java class:
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
public class NumberSet {
private Collection<? extends Number> numbers;
public NumberSet(Collection<? extends Number> numbers) {
this.numbers = numbers;
}
public NumberSet(NumberSet other) {
//copy other.numbers to this.numbers
numbers = new LinkedList<>();
for (Iterator<? extends Number> it = other.numbers.iterator(); it.hasNext();) {
numbers.add(it.next()); // Here's Syntax Error near `it.next()`
}
}
}
There is This Syntax Error inside for
loop:
actual argument Number cannot be converted to CAP#1 by method invocation conversion
where E is a type-variable:
E extends Object declared in interface Collection
where CAP#1 is a fresh type-variable:
CAP#1 extends Number from capture of ? extends Number
I Understand the meaning of PECS
but I want to implement a copy-constructor for this class. the copied instance would be used Just as an snapshot of other
. any Idea?