Reputation: 446
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?
Upvotes: 0
Views: 721
Reputation: 367
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>());
Upvotes: 3