简体   繁体   中英

Java - Add List<Double> to List<List<Object>>

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?

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>());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM