0

I have a function that expects a List of Lists of Objects to fill the rows of a table, the type of data in each row needs to be Object, but when I try to add to the List of Lists a List of type Double, I get the following error

The method add(List<Object>) in the type List<List<Object>> is not applicable for the arguments (List<Double>)

Here an example:

List<List<Object>> rows = new ArrayList<>();
rows.add(new ArrayList<Double>());

I'm guessing that autoboxing doesn't work when dealing with Objects inside a List.

How can I add the list of doubles to the list of lists of objects? I can't modify the function that expects a List of List of objects to expect generic types, so really my question is how can I cast my list of Double to List of Objects without looping through the List and casting each item individually?

Juanpe
  • 446
  • 5
  • 16
  • 4
    Possible duplicate of [Is List a subclass of List? Why are Java generics not implicitly polymorphic?](https://stackoverflow.com/questions/2745265/is-listdog-a-subclass-of-listanimal-why-are-java-generics-not-implicitly-po) – OH GOD SPIDERS Feb 07 '18 at 09:36
  • I don't know what you're asking - there doesn't seem to be a question in your question. But I feel sure that if I did know, this question would be a duplicate of the one that OH GOD SPIDERS has identified. – Dawood ibn Kareem Feb 07 '18 at 09:39
  • What's your doubt or question here? Can you elaborate it – Suresh Feb 07 '18 at 09:50
  • updated question! – Juanpe Feb 07 '18 at 10:53

1 Answers1

3

No, a List of doubles is not a list of objects..

You have to explicitly mention that List accepts any child class of the parent class.. in your case.. You have to do below to fix it..

    List<List<? extends Object>> rows = new ArrayList<>();
    rows.add(new ArrayList<Double>());
Awanish
  • 327
  • 1
  • 3
  • 14
  • Yes but that list is not applicable for the function that expects a list of lists of objects – Juanpe Feb 07 '18 at 11:31