M
martinus
Hi all, I have implemented an interface that looks somewhat like this:
public interface LocalOptimizer {
<X extends List<Node> & RandomAccess> void optimize(final X tour);
<X extends List<Node> & RandomAccess> X doSomething();
}
The method optimize() requires a class that implements List<Node> and
RandomAccess, for example ArrayList does this. This works great, I can
call it like this:
optimize(new ArrayList<Node>());
But I cannot use the second method, this code does not compile:
public <X extends List<Node> & RandomAccess> X doSomething() {
return new ArrayList<Node>();
}
I get the error message "Type mismatch: cannot convert from
ArrayList<Node> to X"
Any ideas how I can make this work?
Martin
public interface LocalOptimizer {
<X extends List<Node> & RandomAccess> void optimize(final X tour);
<X extends List<Node> & RandomAccess> X doSomething();
}
The method optimize() requires a class that implements List<Node> and
RandomAccess, for example ArrayList does this. This works great, I can
call it like this:
optimize(new ArrayList<Node>());
But I cannot use the second method, this code does not compile:
public <X extends List<Node> & RandomAccess> X doSomething() {
return new ArrayList<Node>();
}
I get the error message "Type mismatch: cannot convert from
ArrayList<Node> to X"
Any ideas how I can make this work?
Martin