I hava some problems with my code. First of all I have an interface:
public interface Generator <T, R> {
public T next (R x);
}
Then, I have created the class "DemoClass"
public class DemoClass {
private int id;
public DemoClass (int id){
this.id = id;
}
}
And... Generic Class as well
public class GenericClass implements Generator <DemoClass, Integer> {
public DemoClass next(Integer x) {
return new DemoClass (x);
}
}
After, I have created Main Class and a generic static method that containts a method like Class . I would like to know, is there any opportunity to use such construction like Class ??? My MainClass
import java.util.*;
public class MainClass {
private static Random rand = new Random ();
public static <T> T [] arr (T [] a, Class <?> typeToken){
try{
Generator <?, ?> gen = (Generator <?, ?>)typeToken.newInstance(); // How can I pass two paramets to Generator <?, ?>???
for (int i=0; i!=a.length; i++){
a[i] = (T) gen.next(rand.nextInt(100)); // This line does not work!
}
} catch (Exception e){
throw new RuntimeException (e);
}
return a;
}
public static void main (String [] args){
DemoClass [] myarr = arr (new DemoClass[10], GenericClass.class);
}
}