java - Why does this constructor that uses generics fail? -


i creating class that, @ present, stores lists of various types in internal object called generictable. each list (composed of either double, or long) held in object instance of class genericlist.

question: why doesn't method addvector work?

the error under red underline says the constructor test<v>.genericlist<v>(list<list<v>>) undefined.

if working in main method (but had same genericlist class) , created generictable within main method (using list<genericlist<?>> table = new arraylist<genericlist<?>>();) , did generictable.add(new genericlist<long>(arrays.aslist(genericvector))); (where genericvector in case list<long>), works perfectly.

public class test<v> {      private final list<genericlist<?>> generictable = new arraylist<genericlist<?>>();      public void addvector(list<v> genericvector) {         generictable.add(new genericlist<v>(arrays.aslist(genericvector)));     }      private class genericlist<k> {         private final list<k> listgeneric;              public genericlist(list<k> input) {            listgeneric = input;         }     } } 

you're unnecessarily using arrays.aslist(), when have list. consequently list of lists, not constructr accepts.

see javadocs:

this method provides convenient way create fixed-size list initialized contain several elements:

 list<string> stooges = arrays.aslist("larry", "moe", "curly"); 

so in case you're getting list of lists, instead of list of strings.

i've added bit comments, clarity:

the method signature aslist() this:-

public static <t> list<t> aslist(t... a) 

so because t... vararg, when pass in "larry", "moe", "curly", compiled method receives array of ["larry", "moe", "curly"], , returns them list.

so because passed in list, rather array, method takes vararg array this: [genericvector], , returns array list, , constructor breaks.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -