How to write Java generic method with E... elements and return List<E> -


i want write method follow

public static <e> arraylist<e> newlist(e... elements){     arraylist<e> list = new arraylist<e>(elements.length);     addall(list,elements);     return list; } 

it works when use this

list<string> = listutils.newlist("a","b"); 

but can't compile in case

list<class> c = listutils.newlist(string.class,long.class); 

how can fix problem ? thank !

if understand you, can wildcard (jls-4.5.1) so,

public static void main(string[] vargs) {   list<class<?>> c = listutils.<class<?>>newlist(string.class,long.class); } 

also, newlist wouldn't compile posted it. assume should like,

public static <e> list<e> newlist(e... elements) {   list<e> list = new arraylist<e>((elements != null) ? elements.length : 0);   (e elem : elements) {     list.add(elem);   }   return list; } 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -