I get a ClassCastException
error when I run this because of the implicit cast of d to a double when I run the code. However, if I change reference to d in to Object[]
then it can't be a parameter to the set function. If I change the set function to accept an Object[]
then everything works fine, but then the class will fail at runtime if anyone calls set on an object that isn't of type N
.
I need a solution that can get()
the old array (or a clone thereof) and that can set()
data to a new array.
public class Foo<N> {
public static void main(String[] args) {
Foo<Double> foo = new Foo<Double>();
Double[] d = foo.get();
// do stuff to d ...
foo.set(d);
}
N[] data;
public Foo() {
data = (N[]) new Object[2];
}
public N[] get() {
return (N[]) data;
}
public void set(N[] data) {
this.data = data;
}
}