0

I am creating a class that, at present, stores lists of various types in an internal object called genericTable. Each list (composed of either Double, or Long) are all held in an object which is an instance of class GenericList.

Question: Why doesn't the method addVector work?

The error under the red underline says the constructor Test<V>.GenericList<V>(List<List<V>>) is undefined.

If I was working in a main method (but had the same GenericList class) and created genericTable within the main method (using List<GenericList<?>> Table = new ArrayList<GenericList<?>>();) and did genericTable.add(new GenericList<Long>(Arrays.asList(genericVector))); (where genericVector in this case is a List<Long>), it 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;
        }
    }
}
user2763361
  • 3,789
  • 11
  • 45
  • 81
  • 2
    Please use Java naming conventions. It is very hard to work out what is a `class` and what is a variable when everything starts with an upper-case letter. – Boris the Spider Oct 06 '13 at 14:44

1 Answers1

4

You're unnecessarily using Arrays.asList(), when you already have a list. Consequently you get a list of lists, which is not what the constructr accepts.

See this from the javadocs:

This method also provides a convenient way to create a fixed-size list initialized to contain several elements:

 List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");

So in your case you're getting a list of lists, instead of a list of strings.

I've added this bit from the comments, for clarity:

The method signature for asList() is like this:-

public static <T> List<T> asList(T... a)

So because T... a is a vararg, when you pass in "Larry", "Moe", "Curly", the compiled method actually receives an array of ["Larry", "Moe", "Curly"], and returns them as a List.

So because you passed in a List, rather than an array, the method takes the vararg array like this: [genericVector], and returns that array as a list, and you constructor breaks.

Aaron
  • 652
  • 4
  • 10
  • So all is fine and dandy if I just change the suspect method to `genericTable.add(new GenericList(genericVector));`? This does seem to work. – user2763361 Oct 06 '13 at 14:57
  • @user2763361 I can't run your entire code, but that line was definitely wrong, and the cause of the error you asked about. Your constructor wants a list, and you already had a list, so you should be fine just squirting that list in, rather than using Arrays.asList(). – Aaron Oct 06 '13 at 14:59
  • @user2763361 Normally Arrays.asList() is used to turn an Array into a list. As the snippet i posted shows, you can also use it to get a list from a sequence of individual objects. – Aaron Oct 06 '13 at 15:00
  • @user2763361 Sorry... forgot to add: the second use I mentioned works because varArgs used in a method invocation actually are represented by an array under the hood. So Arrays.asList("Larry", "Moe", "Curly"); is the same as String[] anArray = {"larry", "Moe", "Curly"}; Arrays.asList(anArray); – Aaron Oct 06 '13 at 15:04