Java – a tricky static generic method with a generic return type, which itself can be generic
•
Java
I have a course as follows:
public class MyConverter { public <T> T convert (Object o,String typeidentifier,T dummy) { ... do some conversions such as a java array to an ArrayList or vice versa ... based on a typeidentifier Syntax similar to Class.getName() but which ... embeds information about generic subtypes } }
And hope to do something like this:
int[] ar = {...}; ArrayList<Integer> dummy = null; Integer elem = MyConverter.convert(ar,"java.util.ArrayList<Integer>",dummy) .get(15);
In other words, the T in the transformation itself may be a general instance. I found that to achieve this goal, I must pass a fully typed virtual, because ArrayList Class won't give the java compiler enough information. It's an ArrayList < integer > If I use class < T > dummy LS instead of T dummy
Did I miss anything? Is there a way to write and invoke transformations without virtual?
Solution
Specify the type of call instead of letting Java infer the type:
Integer elem = MyConverter.<ArrayList<Integer>>convert(ar,"java.util.ArrayList<Integer>");
This link describes this (cool) syntax
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码