Wednesday, 15 April 2015

Java create list at runtime -


Here is an example program. By the comment will be unsuccessful, I want to know that I can make a method that achieves this functionality of making a list on runtime.

  import java.util.ArrayList; Import java.util.list; Public category list creator {public static list & lt ;? & Gt; Macklist (class clazes) {// I want to do something like new array list & lt; Clazz & gt; (); } Public static zero main (string [] args) {Vo object = new vo (100); ArrayList & LT; Who & gt; List = Messel (V. Class); List.add (object); }} Class Vo {int i; Public Vo (int i) {this.i = i;}}  

You do not use generic, and you can not really do it in Java, what you are trying to do (to create a Mainlist method, which uses a specific paragraph, gives a specific arrete, which is the form of T Should be used in).

Remember that Generic is just a compiler feature and that for the effect of type extinction, ArrayList & lt; T & gt; will be simplified ArrayList when bytecode is compiled (you will only be able to get information about the original type parameter using reflection sections, which is a powerful method signing on runtime Information is removed).

T in ArrayList & lt; T & gt; This is not a variable, and you can not create a parametrid type on the runtime, as if you are trying to do that you can use a plain old array list without any type of object type being checked .


No comments:

Post a Comment