public class MyGenerics<T extends Number>{
List<T> = new ArrayList<T>();
public void addToList(){
for (ListIterator<T> it; it.hasNext(); ){
it.next();
it.add(number());
}
}
public T number(){
return (T) 5*Math.pi;
}
}
As far as I can tell, there is no way to tell the compiler "I promise, everything that is returned by number() is some object that extends Number and will be cast to type T, whatever it is, because I am casting it.
I am new to generics, so any help is greatly appreciated. I got around this by subclassing the class, but that semi-misses the point of generics.
And please note that the issue isn't really about adding just one value to a list. The issue is the generics logic. (Just to be clear)