public <X> X createData(int n)
{
int[] values = new int[n];
Random rand = new Random();
for(int i = 0; i < n; i++)
{
values[i] = rand.nextInt(100);
}
JOptionPane.showMessageDialog(null, values.length);
return (X )values;
}
Hi there, I have cobbled together a small method to return an int array as a generic type (i think?)
The point of this is, I am to call a users method who is going to generate me some data, at which point I then take that data and invoke a method of their selection.
The issue is, when i call this method how can I convert the generic type to its “proper” type so as it invoke the method with this data.
Typically my process will be
1) invoke the user method to get their data(reflection)
2) use this data to invoke their other method(reflection)
Is what I am doing/trying here possible or in the process of making it a generic return type am i ruining everything?
Thanks
If you wan’t to return a list then I would do it by defining a Supplier interface,
Which can be defined as,
and can be used as,